Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用window.open将参数从父窗口传递到子窗口_Javascript - Fatal编程技术网

Javascript 如何使用window.open将参数从父窗口传递到子窗口

Javascript 如何使用window.open将参数从父窗口传递到子窗口,javascript,Javascript,如何使用window.open从Java脚本中的父窗口向子窗口传递参数 请告诉我任何想法 为子窗口创建html时,在html中创建一个隐藏字段并进入子窗口。什么样的参数?我的意思是,参数是什么? 例如,您可以在url中添加GET值: window.open("http://example.com/script.php?KEY=value") 然后在(PHP中)$\u GET['KEY']中输入“value”将该值作为查询字符串传递 试试下面的 window.open("url.php?para

如何使用window.open从Java脚本中的父窗口向子窗口传递参数


请告诉我任何想法

为子窗口创建html时,在html中创建一个隐藏字段并进入子窗口。

什么样的参数?我的意思是,参数是什么? 例如,您可以在url中添加GET值:

window.open("http://example.com/script.php?KEY=value")

然后在(PHP中)$\u GET['KEY']中输入“value”

将该值作为查询字符串传递

试试下面的

window.open("url.php?param=value&param2=value2");

可以使用查询字符串传递参数,这是一种更好的方法

关于使用窗口传递参数的要求。打开。您可以访问某些元素和全局变量,但不能保留这些值。当您执行以下代码时

popupWindow = window.open(url,'popUpWindow','height=500,width=500'); 
popupWindow.alert("test");

当新窗口打开时,您将看到一个警报。但数据将仅在该警报后加载。因此,即使您能够在子javascript中设置任何全局变量的值,但该值也无法保留,因为打开窗口后页面会加载,因此数据会被刷新。

我最初尝试了以下方法:

父窗口:

 var parms = {arg1: "arg1", arg2:"arg2"};
 var handle = window.open(url);
 // stringifyed to make sure the child window does 
 //not try to access object by reference from child to parent.
 handle.window.parameters = JSON.stringify(parms);
子窗口:

$(document).ready(function(){
    var args = JSON.parse( window.parameters);
    // and so on.
});
问题是,有时会在子窗口准备就绪之前设置该值,因此当子窗口准备就绪时,它将是未定义的

因此,我使用了sessionStorage(注意:这不会跨域工作)

家长:

// the user can cause more than one child window so I give storage a unique id.
var parms = JSON.stringify({arg1: "arg1", arg2:"arg2"});
var storageId = "parms" + String(Date.now());
sessionStorage.setItem(storageId, parms);
window.open(url + "?sid=" + storageId);
这是关于孩子的:

$(document).ready(function(){
    var prmstr = window.location.search.split("=");
    var sid = prmstr[1];
    var args = JSON.parse(sessionStorage.getItem(sid));
    sessionStorage.removeItem(sid);
    // and so on
});

可能的副本可以作为url(querystring)的一部分进行传递。我只是在没有pass参数的情况下尝试这样做。但我想使用windowopen传递参数。如何做。你能告诉我一些想法吗。打开(url,'popupWindow','height=500,width=500,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes');请使用url示例将参数添加为querystring:url=“test.php?param1=value1¶m2=value2”