打开新窗口以post方式提交

来源:互联网 发布:机变英盟玩具淘宝网 编辑:程序博客网 时间:2024/05/20 04:14

window.open以post方式提交

1、按钮准备

[html] view plain copy print?
  1. <input id="btn_amp" type="button" value="大表系统"/>  
[html] view plain copy print?
  1. <input id="btn_amp2" type="button" value="大表系统"/>  

2、js方法
[javascript] view plain copy print?
  1. $(function(){  
  2.     $("#btn_amp").click(function(){  
  3.         var username="<%=username%>",password="<%=password%>";  
  4.         postUrl('<%=ampUrl%>'+'login.do',username,password);  
  5.     });  
  6.     $("#btn_amp2").click(function(){  
  7.         var username="<%=username%>",password="<%=password%>";  
  8.         //var data = '{username:'+username+',password:'+password+'}';  
  9.         openPostWindow('<%=ampUrl%>'+'login.do','大表统一远传',username,password);  
  10.     });  
  11. });  
  12. //以post形式在本页打开  
  13. function postUrl(url,username,password){  
  14.       var f=document.createElement("form");  
  15.       f.action=url;  
  16.       f.method="post";//指定为post  
  17.       f.innerHTML="<input type='hidden' name='username' value='"+username+"'/>"+  
  18.                   "<input type='hidden' name='password' value='"+password+"'/>";  
  19.       document.body.appendChild(f);    
  20.       f.submit()  
  21. }  
  22.   
  23. //以post形式打开新的页面,可以传递多个参数  
  24. function openPostWindow(url,windowname,username,password){           
  25.    var tempForm = document.createElement("form");         
  26.    tempForm.id="tempForm1";           
  27.    tempForm.method="post";          
  28.    tempForm.action=url;         
  29.    //open方法不能设置请求方式,一般网页的post都是通过form来实现的。    
  30.    //如果仅仅模拟form的提交方式,那么open方法里那种可设置窗体属性的参数又不能用。    
  31.    //最后想办法整了这么一个两者结合的方式,将form的target设置成和open的name参数一样的值,通过浏览器自动识别实现了将内容post到新窗口中    
  32.    tempForm.target=windowname;           
  33.    tempForm.innerHTML="<input type='hidden' name='username' value='"+username+"'/>"+  
  34.    "<input type='hidden' name='password' value='"+password+"'/>";    
  35.    // tempForm.attachEvent("onsubmit",function(){ openWindow(name); });     //IE  
  36.    tempForm.addEventListener("onsubmit",function(){ openWindow(windowname); });   //chrome      
  37.    document.body.appendChild(tempForm);            
  38.    //tempForm.fireEvent("onsubmit"); //IE      
  39.    tempForm.dispatchEvent(new Event("onsubmit"));//chrome      
  40.    //必须手动的触发,否则只能看到页面刷新而没有打开新窗口    
  41.    tempForm.submit();         
  42.    document.body.removeChild(tempForm);        
  43. }     
  44. //以post形式打开新的页面,只能传递一个参数  
  45. function openPostWindow2(url, windowname, data)         
  46.   
  47. {         
  48.     
  49.    var tempForm = document.createElement("form");         
  50.     
  51.    tempForm.id="tempForm1";         
  52.     
  53.    tempForm.method="post";         
  54.        
  55.    //url    
  56.    tempForm.action=url;         
  57.    //open方法不能设置请求方式,一般网页的post都是通过form来实现的。    
  58.    //如果仅仅模拟form的提交方式,那么open方法里那种可设置窗体属性的参数又不能用。    
  59.    //最后想办法整了这么一个两者结合的方式,将form的target设置成和open的name参数一样的值,通过浏览器自动识别实现了将内容post到新窗口中    
  60.    tempForm.target=content;         
  61.      
  62.    var hideInput = document.createElement("input");         
  63.     
  64.    hideInput.type="hidden";         
  65.     
  66.    //传入参数名,相当于get请求中的content=    
  67.    hideInput.name= "content";    
  68.     
  69.    //传入传入数据,只传递了一个参数内容,实际可传递多个。    
  70.    hideInput.value= data;       
  71.     
  72.    tempForm.appendChild(hideInput);          
  73.    // tempForm.attachEvent("onsubmit",function(){ openWindow(name); });     //IE  
  74.    tempForm.addEventListener("onsubmit",function(){ openWindow(windowname); });   //chrome    
  75.     
  76.    document.body.appendChild(tempForm);         
  77.      
  78.    //tempForm.fireEvent("onsubmit"); //IE      
  79.    tempForm.dispatchEvent(new Event("onsubmit"));//chrome    
  80.     
  81.    //必须手动的触发,否则只能看到页面刷新而没有打开新窗口    
  82.    tempForm.submit();       
  83.     
  84.    document.body.removeChild(tempForm);       
  85.     
  86. }       
  87. function openWindow(name)         
  88. {         
  89.     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');            
  90. }     
原创粉丝点击