完美解决window.open模拟表单POST提交

来源:互联网 发布:济南泉方pubmed数据库 编辑:程序博客网 时间:2024/05/16 14:16

1.案例:IE浏览器地址栏长度限制导致传参不完整


2.目的:解决地址栏长度限制,隐藏了参数,不在地址栏显示


3.方案

将form的target设置成和open的name参数一样的值,通过浏览器自动识别实现了将内容post到新窗口中


        var url="secnequipretired/scrap/add/applyScrap/pc";        var tempForm = document.createElement("form");                   tempForm.id="tempForm";                   tempForm.method="post";                      tempForm.action=url;               tempForm.target="blank";                   var hideInput = document.createElement("input");                   hideInput.type="hidden";                   hideInput.name="ids";          hideInput.value= ids;                 tempForm.appendChild(hideInput);        if (tempForm.attachEvent) {  // IE                 tempForm.attachEvent("onsubmit",function(){ window.open('about:blank','blank'); });          } else if (tempForm.addEventListener) {  // DOM Level 2 standard                  tempForm.addEventListener("onsubmit",function(){ window.open('about:blank','blank'); });          }                      document.body.appendChild(tempForm);           if (document.createEvent) { // DOM Level 2 standard                 evt = document.createEvent("MouseEvents");                 evt.initMouseEvent("submit", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);                 tempForm.dispatchEvent(evt);          } else if (tempForm.fireEvent) { // IE                 tempForm.fireEvent('onsubmit');          }       //必须手动的触发                tempForm.submit();                 document.body.removeChild(tempForm);


1 0