javascript:使用window.open()发送自定义参数,但不起作用

javascript:使用window.open()发送自定义参数,但不起作用,javascript,jquery,Javascript,Jquery,如果可能的话,有人能帮我吗 抱歉,细节不完整,这是一个Post请求 如果要传递POST变量,必须使用HTML表单: 1. window.open("http://localhost:8080/login?cid='username'&pwd='password'","mywindow") 2. window.open("http://localhost:8080/login","mywindow") mywindow.getElementById('cid').value=

如果可能的话,有人能帮我吗


抱歉,细节不完整,这是一个Post请求

如果要传递POST变量,必须使用HTML表单:

1.  window.open("http://localhost:8080/login?cid='username'&pwd='password'","mywindow")

2.  window.open("http://localhost:8080/login","mywindow")
    mywindow.getElementById('cid').value='MyUsername'
    mywindow.getElementById('pwd').value='mypassword'
下面是如何使用javascrpt变量创建上面的字符串:

http://yourdomain.com/login?cid=username&pwd=password
在具有该url的文档中,必须读取GET参数。如果是php,请使用:

myu = document.getElementById('cid').value;
myp = document.getElementById('pwd').value;
window.open("http://localhost:8080/login?cid="+ myu +"&pwd="+ myp ,"MyTargetWindowName");

请注意:以这种方式传输密码是一个巨大的安全漏洞

要连接字符串,请使用
+
运算符

要将数据插入URI,请对其进行URI编码

坏的:

好:

服务器必须处理查询字符串才能使用数据。您不能指定给任意表单字段


…但不要触发新窗口或在URI中传递凭据(在URI中,它们会受到越级攻击并可能被记录)。

您可以使用此选项,但仍然存在安全问题

var url_safe_username = encodeURIComponent(username);
var url_safe_password = encodeURIComponent(password);
var url = "http://localhost:8080/login?cid=" + url_safe_username + "&pwd=" + url_safe_password;

函数fnc1()
{
var a=window.location.href;
username=“p”;
密码=1234;
window.open(a+'?用户名='+username+'&密码='+password,“”);
}   

注意:不要使用此方法传递用户名、密码等敏感信息。

请查找此示例代码,您可以使用隐藏表单和POST将数据发送到您的URL,如下所示:

You can try this instead


var myu = document.getElementById('myu').value;
var myp = document.getElementById('myp').value;
window.opener.location.href='myurl.php?myu='+ myu +'&myp='+ myp;

我发现这个方法非常有用,我希望这对很多用户也有帮助

//-->屏幕1:

function open_win()
{
    var ChatWindow_Height = 650;
    var ChatWindow_Width = 570;

    window.open("Live Chat", "chat", "height=" + ChatWindow_Height + ", width = " + ChatWindow_Width);

    //Hidden Form
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", "http://localhost:8080/login");
    form.setAttribute("target", "chat");

    //Hidden Field
    var hiddenField1 = document.createElement("input");
    var hiddenField2 = document.createElement("input");

    //Login ID
    hiddenField1.setAttribute("type", "hidden");
    hiddenField1.setAttribute("id", "login");
    hiddenField1.setAttribute("name", "login");
    hiddenField1.setAttribute("value", "PreethiJain005");

    //Password
    hiddenField2.setAttribute("type", "hidden");
    hiddenField2.setAttribute("id", "pass");
    hiddenField2.setAttribute("name", "pass");
    hiddenField2.setAttribute("value", "Pass@word$");

    form.appendChild(hiddenField1);
    form.appendChild(hiddenField2);

    document.body.appendChild(form);
    form.submit();

}
这将打开
屏幕2
选项卡。在那里,您可以获得如下传递的值:

//-->屏幕2:

var url = 'screen_2_url';
var id = 'some_ID';


window.open(url + '?id=' + id);

您知道吗,通过将密码字段传递到
窗口。open
是危险的,因为它未加密,并且在每个人都可以看到的普通视图中。GET变量来自JavaScript变量
var url_safe_username = encodeURIComponent(username);
var url_safe_password = encodeURIComponent(password);
var url = "http://localhost:8080/login?cid=" + url_safe_username + "&pwd=" + url_safe_password;
<script type="text/javascript">
function fnc1()
{
    var a=window.location.href;

    username="p";
    password=1234;
    window.open(a+'?username='+username+'&password='+password,"");

}   
</script>
<input type="button" onclick="fnc1()" />
<input type="text" id="atext"  />
You can try this instead


var myu = document.getElementById('myu').value;
var myp = document.getElementById('myp').value;
window.opener.location.href='myurl.php?myu='+ myu +'&myp='+ myp;
function open_win()
{
    var ChatWindow_Height = 650;
    var ChatWindow_Width = 570;

    window.open("Live Chat", "chat", "height=" + ChatWindow_Height + ", width = " + ChatWindow_Width);

    //Hidden Form
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", "http://localhost:8080/login");
    form.setAttribute("target", "chat");

    //Hidden Field
    var hiddenField1 = document.createElement("input");
    var hiddenField2 = document.createElement("input");

    //Login ID
    hiddenField1.setAttribute("type", "hidden");
    hiddenField1.setAttribute("id", "login");
    hiddenField1.setAttribute("name", "login");
    hiddenField1.setAttribute("value", "PreethiJain005");

    //Password
    hiddenField2.setAttribute("type", "hidden");
    hiddenField2.setAttribute("id", "pass");
    hiddenField2.setAttribute("name", "pass");
    hiddenField2.setAttribute("value", "Pass@word$");

    form.appendChild(hiddenField1);
    form.appendChild(hiddenField2);

    document.body.appendChild(form);
    form.submit();

}
var url = 'screen_2_url';
var id = 'some_ID';


window.open(url + '?id=' + id);
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const id = urlParams.get('id');