window.open以post方式提交

第一种方式

最近在做web项目,碰到需要跨页面传递参数的功能,就是那种需要把当前页面的内容带到新开的子窗体中,以前的做法是传一个id过去,然后在新窗口中去读数据库的内容。虽然不怎么麻烦,但是如果内容么有在数据库里保存,仅仅是处以拟稿状态时,就不能实现了,用户还常常认为是个bug。考虑采用get的方式传递,把需要的内容都序列化然后,通过url去传,显得很臃肿,而且get的传递内容长度有限制。于是就想到用post的方式传递,问题在于open方法不能设置请求方式,一般网页的post都是通过form来实现的。如果仅仅模拟form的提交方式,那么open方法里那种可设置窗体属性的参数又不能用。最后想办法整了这么一个两者结合的方式,将form的target设置成和open的name参数一样的值,通过浏览器自动识别实现了将内容post到新窗口中。
比较有意思的是直接通过调用form的submit方法不能触发onsubmit事件,查看了帮助文档,必须手动的触发,否则只能看到页面刷新而没有打开新窗口。代码中只传递了一个参数内容,实际可传递多个。
具体代码如下:

Java代码 复制代码 收藏代码
  1. <script>
  2. function openPostWindow(url, data, name)
  3. {
  4. var tempForm = document.createElement("form");
  5. tempForm.id="tempForm1";
  6. tempForm.method="post";
  7. tempForm.action=url;
  8. tempForm.target=name;
  9. var hideInput = document.createElement("input");
  10. hideInput.type="hidden";
  11. hideInput.name= "content"
  12. hideInput.value= data;
  13. tempForm.appendChild(hideInput);
  14. tempForm.attachEvent("onsubmit",function(){ openWindow(name); });
  15. document.body.appendChild(tempForm);
  16. tempForm.fireEvent("onsubmit");
  17. tempForm.submit();
  18. document.body.removeChild(tempForm);
  19. }
  20. function openWindow(name)
  21. {
  22. window.open('about:blank',name,'height=400, width=400, top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes');
  23. }
  24. </script>
<script>

function openPostWindow(url, data, name)  

  {  

     var tempForm = document.createElement("form");  

     tempForm.id="tempForm1";  

     tempForm.method="post";  

     tempForm.action=url;  

     tempForm.target=name;  

  

     var hideInput = document.createElement("input");  

     hideInput.type="hidden";  

     hideInput.name= "content"

     hideInput.value= data;

     tempForm.appendChild(hideInput);   

     tempForm.attachEvent("onsubmit",function(){ openWindow(name); });

     document.body.appendChild(tempForm);  



     tempForm.fireEvent("onsubmit");

     tempForm.submit();

     document.body.removeChild(tempForm);

}



function openWindow(name)  

{  

     window.open('about:blank',name,'height=400, width=400, top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes');   

}  

</script>

第二种方式

Java代码 复制代码 收藏代码
  1. function openWindowWithPost(url,name,keys,values)
  2. {
  3. var newWindow = window.open(url, name);
  4. if (!newWindow)
  5. return false;
  6. var html = "";
  7. html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";
  8. if (keys && values)
  9. {
  10. html += "<input type='hidden' name='" + keys + "' value='" + values + "'/>";
  11. }
  12. html += "</form><script type='text/javascript'>document.getElementById('formid').submit();";
  13. html += "<\/script></body></html>".toString().replace(/^.+?\*|\\(?=\/)|\*.+?$/gi, "");
  14. newWindow.document.write(html);
  15. return newWindow;
  16. }
function openWindowWithPost(url,name,keys,values)
{
    var newWindow = window.open(url, name);
    if (!newWindow)
        return false;
        
    var html = "";
    html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";
    if (keys && values)
    {
       html += "<input type='hidden' name='" + keys + "' value='" + values + "'/>";
    }
    
    html += "</form><script type='text/javascript'>document.getElementById('formid').submit();";
    html += "<\/script></body></html>".toString().replace(/^.+?\*|\\(?=\/)|\*.+?$/gi, ""); 
    newWindow.document.write(html);
    
    return newWindow;
}

推荐使用第一种方式,第二种方式测试过程中发现,与日历弹出框冲突,如果子页面上有日历弹出框,则点日历框会不停刷新页面,不会弹出日历框。当然,也可能是我用的日历框的问题。

http://yuxuguang.iteye.com/blog/895108

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值