window.open()實(shí)現(xiàn)post傳遞參數(shù)
在實(shí)際項(xiàng)目中,常常遇到這樣的需求,即實(shí)現(xiàn)子系統(tǒng)頁(yè)面之間跳轉(zhuǎn)并在新的頁(yè)面打開(kāi),我所在項(xiàng)目組使用的是SSH框架,所以u(píng)rl均為類似****.action,同時(shí)還帶有兩參數(shù)(系統(tǒng)ID與系統(tǒng)名稱),兩個(gè)參數(shù)被struts攔截后存入session中,在打開(kāi)的子系統(tǒng)頁(yè)面中還有個(gè)ztree插件實(shí)現(xiàn)的樹(shù)狀菜單需要參數(shù)系統(tǒng)ID才能初始化,直接使用window.open(url,"_blank"),會(huì)使得url長(zhǎng)度過(guò)長(zhǎng),同時(shí)還暴露一些參數(shù)。故想改用post方式提交,隱藏提交過(guò)程中參數(shù)的傳遞。首先想到ajax提交,但是兩個(gè)參數(shù)的傳遞會(huì)存在問(wèn)題,ajax提交與window.open()會(huì)使得action走兩遍,因此舍去。后又重新認(rèn)真看了window.open()的API,鏈接地址http://www.w3school.com.cn/jsref/met_win_open.asp。window.open()默認(rèn)是get提交方式,想要實(shí)現(xiàn)post提交方式,還得另想它法。參考http://chabaoo.cn/article/32826.htm,這里介紹了一種方法。也是常被采用的方法。我根據(jù)實(shí)際情況略作修改:
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ù)個(gè)數(shù)根據(jù)實(shí)際需要自行修改。data1與data2為action需要傳遞的參數(shù)。此外,此處還需考慮Javascript事件瀏覽器兼容問(wèn)題。我這里注釋了function openWindow(),不然會(huì)多打開(kāi)一個(gè)空白頁(yè)面(about:blank)。這樣基本滿足需求了。
以上就是本文分享的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
JS實(shí)現(xiàn)獲取圖片大小和預(yù)覽的方法完整實(shí)例【兼容IE和其它瀏覽器】
這篇文章主要介紹了JS實(shí)現(xiàn)獲取圖片大小和預(yù)覽的方法,結(jié)合完整實(shí)例形式分析了javascript針對(duì)不同瀏覽器處理圖片上傳與預(yù)覽等操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-04-04JS函數(shù)式編程之純函數(shù)、柯里化以及組合函數(shù)
這篇文章主要介紹了JS函數(shù)式編程之純函數(shù)、柯里化以及組合函數(shù),文章對(duì)三個(gè)函數(shù)進(jìn)行分析講解,內(nèi)容也很容易理解,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01原生JS實(shí)現(xiàn)垂直手風(fēng)琴效果
本篇文章主要介紹了原生JS實(shí)現(xiàn)垂直手風(fēng)琴效果的示例代碼,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02開(kāi)發(fā) Internet Explorer 右鍵功能表(ContextMenu)
本篇介紹如何開(kāi)發(fā) Internet Explorer 右鍵功能表(ContextMenu),以 0rz.tw 縮短網(wǎng)址列為范例2013-07-07