window.open()使用post方式提交

转自:http://aguu125.iteye.com/blog/1545737

 

一般 

window.open 传参数都是用Get..方式..在url后面拼接参数… 

有时候并不适用,如: 

1)不想被看到参数信息 

2)参数过长,get有限制会被截断 

3)可能有中文编码问题 

所以需要用post方式 

方式一:

 

 
    1. function submitForm(){

    2. window.open(‘’,’newWin’,’width=400,height=500,scrollbars=yes’);

    3. form对象.submit();

    4. }

    5. <FORM name=form action=“YourActionFile.html” method=“post” target=“newWin”>

    6. </Form>

 

也可

 

 
    1. function openWindowWithPost(url,name,keys,values)

    2. {

    3. var newWindow = window.open(url, name);

    4. if (!newWindow)

    5. return false;

    6.  
    7. var html = “”;

    8. html += “<html><head></head><body><form id=‘formid’ method=‘post’ action=‘“ + url + “‘>“;

    9. if (keys && values)

    10. {

    11. html += “<input type=‘hidden’ name=‘“ + keys + “‘ value=‘“ + values + “‘/>“;

    12. }

    13.  
    14. html += “</form><script type=‘text/javascript’>document.getElementById(‘formid’).submit();”;

    15. html += “<\/script></body></html>“.toString().replace(/^.+?*|\(?=\/)|*.+?$/gi, “”);

    16. newWindow.document.write(html);

    17.  
    18. return newWindow;

    19. }

    20.  

 


方式二:

 

 
    1. <script>

    2.  
    3. function openPostWindow(url, data, name)

    4.  
    5. {

    6.  
    7. var tempForm = document.createElement(“form”);

    8.  
    9. tempForm.id=“tempForm1”;

    10.  
    11. tempForm.method=“post”;

    12.  
    13. //url

    14. tempForm.action=url;

    15. //open方法不能设置请求方式,一般网页的post都是通过form来实现的。

    16. //如果仅仅模拟form的提交方式,那么open方法里那种可设置窗体属性的参数又不能用。

    17. //最后想办法整了这么一个两者结合的方式,将form的target设置成和open的name参数一样的值,通过浏览器自动识别实现了将内容post到新窗口中

    18. tempForm.target=name;

    19.  
    20.  
    21.  
    22. var hideInput = document.createElement(“input”);

    23.  
    24. hideInput.type=“hidden”;

    25.  
    26. //传入参数名,相当于get请求中的content=

    27. hideInput.name= “content”;

    28.  
    29. //传入传入数据,只传递了一个参数内容,实际可传递多个。

    30. hideInput.value= data;

    31.  
    32. tempForm.appendChild(hideInput);

    33.  
    34. tempForm.attachEvent(“onsubmit”,function(){ openWindow(name); });

    35.  
    36. document.body.appendChild(tempForm);

    37.  
    38.  
    39.  
    40. tempForm.fireEvent(“onsubmit”);

    41.  
    42. //必须手动的触发,否则只能看到页面刷新而没有打开新窗口

    43. tempForm.submit();

    44.  
    45. document.body.removeChild(tempForm);

    46.  
    47. }

    48.  
    49.  
    50.  
    51. function openWindow(name)

    52.  
    53. {

    54.  
    55. 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’);

    56.  
    57. }

    58.  
    59. </script>

 


推荐使用第二种方式,第一种方式有时候有问题

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值