window.open()實現post傳遞參數,window.openpost

來源:互聯網
上載者:User

window.open()實現post傳遞參數,window.openpost

在實際項目中,常常遇到這樣的需求,即實現子系統頁面之間跳轉並在新的頁面開啟,我所在項目組使用的是SSH架構,所以url均為類似****.action,同時還帶有兩參數(系統ID與系統名稱),兩個參數被struts攔截後存入session中,在開啟的子系統頁面中還有個ztree外掛程式實現的樹狀菜單需要參數系統ID才能初始化,直接使用window.open(url,"_blank"),會使得url長度過長,同時還暴露一些參數。故想改用post方式提交,隱藏提交過程中參數的傳遞。首先想到ajax提交,但是兩個參數的傳遞會存在問題,ajax提交與window.open()會使得action走兩遍,因此捨去。後又重新認真看了window.open()的API,連結地址http://www.w3school.com.cn/jsref/met_win_open.asp。window.open()預設是get提交方式,想要實現post提交方式,還得另想它法。參考http://www.bkjia.com/article/32826.htm,這裡介紹了一種方法。也是常被採用的方法。我根據實際情況略作修改:

複製代碼 代碼如下:
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()函數中的參數個數根據實際需要自行修改。data1與data2為action需要傳遞的參數。此外,此處還需考慮Javascript事件瀏覽器安全色問題。我這裡注釋了function openWindow(),不然會多開啟一個空白頁面(about:blank)。這樣基本滿足需求了。

以上就是本文分享的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.