window.open()實(shí)現(xiàn)post傳遞參數(shù)
在實(shí)際項(xiàng)目中,常常遇到這樣的需求,即實(shí)現(xiàn)子系統(tǒng)頁面之間跳轉(zhuǎn)并在新的頁面打開,我所在項(xiàng)目組使用的是SSH框架,所以u(píng)rl均為類似****.action,同時(shí)還帶有兩參數(shù)(系統(tǒng)ID與系統(tǒng)名稱),兩個(gè)參數(shù)被struts攔截后存入session中,在打開的子系統(tǒng)頁面中還有個(gè)ztree插件實(shí)現(xiàn)的樹狀菜單需要參數(shù)系統(tǒng)ID才能初始化,直接使用window.open(url,"_blank"),會(huì)使得url長度過長,同時(shí)還暴露一些參數(shù)。故想改用post方式提交,隱藏提交過程中參數(shù)的傳遞。首先想到ajax提交,但是兩個(gè)參數(shù)的傳遞會(huì)存在問題,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://www.fzitv.net/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事件瀏覽器兼容問題。我這里注釋了function openWindow(),不然會(huì)多打開一個(gè)空白頁面(about:blank)。這樣基本滿足需求了。
以上就是本文分享的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
js跳轉(zhuǎn)到指定url的方法與實(shí)際使用
這篇文章主要給大家介紹了關(guān)于js跳轉(zhuǎn)到指定url的方法與實(shí)際使用的相關(guān)資料,要實(shí)現(xiàn)JavaScript跳轉(zhuǎn)到指定URL,可以使用window.location對(duì)象來實(shí)現(xiàn),需要的朋友可以參考下2023-09-09
JavaScript實(shí)現(xiàn)網(wǎng)站訪問次數(shù)統(tǒng)計(jì)代碼
每個(gè)網(wǎng)站管理者,都必須知道每天有多少人訪問了本站,需要一個(gè)網(wǎng)站訪問次數(shù)功能來滿足需求,本篇文章主要介紹了JavsScript實(shí)現(xiàn)網(wǎng)站訪問次數(shù)統(tǒng)計(jì)代碼,需要的朋友可以參考下2015-08-08
JavaScript 井字棋人工智能實(shí)現(xiàn)代碼
JavaScript fights back in this artificial Tic Tac Toe game. Great script to have to entertain yourself and your visitors.2009-12-12
Javascript Function.prototype.bind詳細(xì)分析
這篇文章主要介紹了Javascript Function.prototype.bind詳細(xì)分析的相關(guān)資料,需要的朋友可以參考下2016-12-12
基于JavaScript實(shí)現(xiàn)淘寶商品廣告效果
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)淘寶商品廣告效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
layui表格內(nèi)放置圖片,并點(diǎn)擊放大的實(shí)例
今天小編就為大家分享一篇layui表格內(nèi)放置圖片,并點(diǎn)擊放大的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09

