window.open模拟表单POST提交


解决地址栏长度限制,隐藏参数,不在地址栏显示
项目 excel 导出中用到
将form的target设置成和open的name参数一样的值,通过浏览器自动识别实现了将内容post到新窗口中

var url=“XXX”;
var tempForm = document.createElement("form");           
tempForm.id="tempForm";           
tempForm.method="post";              
tempForm.action=url;       
tempForm.target="blank";           
//可以定义多个input 传多个参数
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);

 

posted @ 2019-03-27 15:32  ♞⚉♘  阅读(467)  评论(0编辑  收藏  举报