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; }
function openPostWindow(url, data, name) { var tempForm = document.createElement("form"); tempForm.id="tempForm1"; tempForm.method="post"; //url tempForm.action=url; //open方法不能設置請求方式,一般網頁的post都是通過form來實現的。 //如果僅僅模擬form的提交方式,那么open方法里那種可設置窗體屬性的參數又不能用。 //最后想辦法整了這么一個兩者結合的方式,將form的target設置成和open的name參數一樣的值,通過瀏覽器自動識別實現了將內容post到新窗口中 tempForm.target=name; var hideInput = document.createElement("input"); hideInput.type="hidden"; //傳入參數名,相當於get請求中的content= 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'); }