您好,欢迎访问一九零五行业门户网

window.open()实现post传递参数_javascript技巧

在实际项目中,常常遇到这样的需求,即实现子系统页面之间跳转并在新的页面打开,我所在项目组使用的是ssh框架,所以url均为类似****.action,同时还带有两参数(系统id与系统名称),两个参数被struts拦截后存入session中,在打开的子系统页面中还有个ztree插件实现的树状菜单需要参数系统id才能初始化,直接使用window.open(url,_blank),会使得url长度过长,同时还暴露一些参数。故想改用post方式提交,隐藏提交过程中参数的传递。首先想到ajax提交,但是两个参数的传递会存在问题,ajax提交与window.open()会使得action走两遍,因此舍去。后又重新认真看了window.open()的api,链接地址http://www.w3school.com.cn/jsref/met_win_open.asp。window.open()默认是get提交方式,想要实现post提交方式,还得另想它法。参考http://www.jb51.net/article/32826.htm,这里介绍了一种方法。也是常被采用的方法。我根据实际情况略作修改:
复制代码 代码如下:
function openpostwindow(url, name, data1, data2){
    var tempform = document.createelement(form);
    tempform.id = tempform1;
    tempform.method = post;
    tempform.action = url;
    tempform.target=name;
    var hideinput1 = document.createelement(input);
    hideinput1.type = hidden;
    hideinput1.name=xtid;
    hideinput1.value = data1;
    var hideinput2 = document.createelement(input);
    hideinput2.type = hidden;
    hideinput2.name=xtmc;
    hideinput2.value = data2;
    tempform.appendchild(hideinput1);
    tempform.appendchild(hideinput2);
    if(document.all){
        tempform.attachevent(onsubmit,function(){});        //ie
    }else{
        var subobj = tempform.addeventlistener(submit,function(){},false);    //firefox
    }
    document.body.appendchild(tempform);
    if(document.all){
        tempform.fireevent(onsubmit);
    }else{
        tempform.dispatchevent(new event(submit));
    }
    tempform.submit();
    document.body.removechild(tempform);
}
//function openwindow(name){
//    window.open(,name);
//}
openpostwindow()函数中的参数个数根据实际需要自行修改。data1与data2为action需要传递的参数。此外,此处还需考虑javascript事件浏览器兼容问题。我这里注释了function openwindow(),不然会多打开一个空白页面(about:blank)。这样基本满足需求了。
以上就是本文分享的全部内容了,希望大家能够喜欢。
其它类似信息

推荐信息