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.   
  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.      tempForm.action=url;     
  14.   
  15.      tempForm.target=name;     
  16.   
  17.      
  18.   
  19.      var hideInput = document.createElement("input");     
  20.   
  21.      hideInput.type="hidden";     
  22.   
  23.      hideInput.name= "content"  
  24.   
  25.      hideInput.value= data;   
  26.   
  27.      tempForm.appendChild(hideInput);      
  28.   
  29.      tempForm.attachEvent("onsubmit",function(){ openWindow(name); });   
  30.   
  31.      document.body.appendChild(tempForm);     
  32.   
  33.   
  34.   
  35.      tempForm.fireEvent("onsubmit");   
  36.   
  37.      tempForm.submit();   
  38.   
  39.      document.body.removeChild(tempForm);   
  40.   
  41. }   
  42.   
  43.   
  44.   
  45. function openWindow(name)     
  46.   
  47. {     
  48.   
  49.      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');      
  50.   
  51. }     
  52.   
  53. </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.            
  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. }  
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;
}

 

 

推薦使用第一種方式,第二種方式測試過程中發現,與日歷彈出框沖突,如果子頁面上有日歷彈出框,則點日歷框會不停刷新頁面,不會彈出日歷框。當然,也可能是我用的日歷框的問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM