window.open post传参

前言

我使用的场景是,点击弹窗,然后把我当前用户的消息传过去

获取当前用户信息

打开Chrome浏览器,在application那里可以看到cookie的其实

793293-20190618144312572-1061967840.png

通过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,"随便起的名字");

转载于:https://www.cnblogs.com/yunquan/p/11045102.html

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值