window.open post傳參


前言

我使用的場景是,點擊彈窗,然后把我當前用戶的消息傳過去

獲取當前用戶信息

打開Chrome瀏覽器,在application那里可以看到cookie的其實

通過Cookie獲取當前用戶的姓名和郵箱

        var ca = document.cookie.split(';');
        var name = '';
        var email = '';
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i].trim();
            if (c.indexOf('name') == 0) {
                accountContactName = c.substring(19, c.length);
            } else if (c.indexOf('email') == 0) {
                accountName = c.substring(12, c.length);
            }
        }

使用window.open的兩種方式

Get方式

這種方式很簡單,但是不推薦使用,為什么呢?因為你的參數全部都顯示在了url里面,信息暴露了

window.open("http://test.com/controller/Index?name=" + name + "&email=" + email ,"", "width=810,height=630,top=100,left=200")

Post方式

這種方式好用,先寫兩個js方法

    function openPostWindow(url, username, useremail, name) {  //url要跳轉到的頁面,data要傳遞的數據,name顯示方式(可能任意命名)
        var tempForm = $("<form>");
        tempForm.attr("id", "tempForm1");
        tempForm.attr("style", "display:none");
        tempForm.attr("target", name);
        tempForm.attr("method", "post");
        tempForm.attr("action", url);
        var input1 = $("<input>");
        input1.attr("type", "hidden");
        input1.attr("name", "username");
        input1.attr("value", username);
        var input2 = $("<input>");
        input2.attr("type", "hidden");
        input2.attr("name", "useremail");
        input2.attr("value", useremail);
        tempForm.append(input1);
        tempForm.append(input2);
        tempForm.on("submit", function () { openWindow(name); }); // 必須用name不能只用url,否則無法傳值到新頁面
        tempForm.trigger("submit");
        $("body").append(tempForm);//將表單放置在web中
        tempForm.submit();
        $("tempForm1").remove();
    };


    function openWindow(name) {   
        window.open('about:blank', name, "width=810,height=630,top=100,left=200,toolbar=no, menubar=no,  scrollbars=yes,resizable=yes,location=no, status=no");
    };

然后調用的時候這樣調用

openPostWindow('http://test.com/controller/Index', name, email,"隨便起的名字");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM