window.open()實現(xiàn)post傳遞參數(shù)
在實際項目中,常常遇到這樣的需求,即實現(xiàn)子系統(tǒng)頁面之間跳轉并在新的頁面打開,我所在項目組使用的是SSH框架,所以url均為類似****.action,同時還帶有兩參數(shù)(系統(tǒng)ID與系統(tǒng)名稱),兩個參數(shù)被struts攔截后存入session中,在打開的子系統(tǒng)頁面中還有個ztree插件實現(xiàn)的樹狀菜單需要參數(shù)系統(tǒng)ID才能初始化,直接使用window.open(url,"_blank"),會使得url長度過長,同時還暴露一些參數(shù)。故想改用post方式提交,隱藏提交過程中參數(shù)的傳遞。首先想到ajax提交,但是兩個參數(shù)的傳遞會存在問題,ajax提交與window.open()會使得action走兩遍,因此舍去。后又重新認真看了window.open()的API,鏈接地址http://www.w3school.com.cn/jsref/met_win_open.asp。window.open()默認是get提交方式,想要實現(xiàn)post提交方式,還得另想它法。參考http://www.dbjr.com.cn/article/32826.htm,這里介紹了一種方法。也是常被采用的方法。我根據(jù)實際情況略作修改:
function openPostWindow(url, name, data1, data2){
var tempForm = document.createElement("form");
tempForm.id = "tempForm1";
tempForm.method = "post";
tempForm.action = url;
tempForm.target=name;
var hideInput1 = document.createElement("input");
hideInput1.type = "hidden";
hideInput1.name="xtid";
hideInput1.value = data1;
var hideInput2 = document.createElement("input");
hideInput2.type = "hidden";
hideInput2.name="xtmc";
hideInput2.value = data2;
tempForm.appendChild(hideInput1);
tempForm.appendChild(hideInput2);
if(document.all){
tempForm.attachEvent("onsubmit",function(){}); //IE
}else{
var subObj = tempForm.addEventListener("submit",function(){},false); //firefox
}
document.body.appendChild(tempForm);
if(document.all){
tempForm.fireEvent("onsubmit");
}else{
tempForm.dispatchEvent(new Event("submit"));
}
tempForm.submit();
document.body.removeChild(tempForm);
}
//function openWindow(name){
// window.open("",name);
//}
openPostWindow()函數(shù)中的參數(shù)個數(shù)根據(jù)實際需要自行修改。data1與data2為action需要傳遞的參數(shù)。此外,此處還需考慮Javascript事件瀏覽器兼容問題。我這里注釋了function openWindow(),不然會多打開一個空白頁面(about:blank)。這樣基本滿足需求了。
以上就是本文分享的全部內容了,希望大家能夠喜歡。
相關文章
JS實現(xiàn)獲取圖片大小和預覽的方法完整實例【兼容IE和其它瀏覽器】
這篇文章主要介紹了JS實現(xiàn)獲取圖片大小和預覽的方法,結合完整實例形式分析了javascript針對不同瀏覽器處理圖片上傳與預覽等操作的相關實現(xiàn)技巧,需要的朋友可以參考下2017-04-04
JS函數(shù)式編程之純函數(shù)、柯里化以及組合函數(shù)
這篇文章主要介紹了JS函數(shù)式編程之純函數(shù)、柯里化以及組合函數(shù),文章對三個函數(shù)進行分析講解,內容也很容易理解,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2023-01-01
開發(fā) Internet Explorer 右鍵功能表(ContextMenu)
本篇介紹如何開發(fā) Internet Explorer 右鍵功能表(ContextMenu),以 0rz.tw 縮短網(wǎng)址列為范例2013-07-07

