我可以传递一个 JavaScript 变量到另一个浏览器窗口吗?

我有一个页面,产生一个弹出式浏览器窗口。我在父浏览器窗口中有一个 JavaScript 变量,我想把它传递给弹出的浏览器窗口。

有办法吗?我知道这可以在同一个浏览器窗口中跨框架实现,但我不确定是否可以在浏览器窗口中实现。

161372 次浏览

The window.open() function will also allow this if you have a reference to the window created, provided it is on the same domain. If the variable is used server side you should be using a $_SESSION variable (assuming you are using PHP).

Provided the windows are from the same security domain, and you have a reference to the other window, yes.

Javascript's open() method returns a reference to the window created (or existing window if it reuses an existing one). Each window created in such a way gets a property applied to it "window.opener" pointing to the window which created it.

Either can then use the DOM (security depending) to access properties of the other one, or its documents,frames etc.

Yes, it can be done as long as both windows are on the same domain. The window.open() function will return a handle to the new window. The child window can access the parent window using the DOM element "opener".

Alternatively, you can add it to the URL and let the scripting language (PHP, Perl, ASP, Python, Ruby, whatever) handle it on the other side. Something like:

var x = 10;
window.open('mypage.php?x='+x);

In your parent window:

var yourValue = 'something';
window.open('/childwindow.html?yourKey=' + yourValue);

Then in childwindow.html:

var query = location.search.substring(1);
var parameters = {};
var keyValues = query.split(/&/);
for (var keyValue in keyValues) {
var keyValuePairs = keyValue.split(/=/);
var key = keyValuePairs[0];
var value = keyValuePairs[1];
parameters[key] = value;
}


alert(parameters['yourKey']);

There is potentially a lot of error checking you should be doing in the parsing of your key/value pairs but I'm not including it here. Maybe someone can provide a more inclusive Javascript query string parsing routine in a later answer.

Putting code to the matter, you can do this from the parent window:

var thisIsAnObject = {foo:'bar'};
var w = window.open("http://example.com");
w.myVariable = thisIsAnObject;

or this from the new window:

var myVariable = window.opener.thisIsAnObject;

I prefer the latter, because you will probably need to wait for the new page to load anyway, so that you can access its elements, or whatever you want.

Yes, scripts can access properties of other windows in the same domain that they have a handle on (typically gained through window.open/opener and window.frames/parent). It is usually more manageable to call functions defined on the other window rather than fiddle with variables directly.

然而,Windows可能会消亡,也可能会继续前进,而浏览器在这样做的时候会以不同的方式处理。在尝试调用窗口(A)之前,请检查窗口(A)是否仍处于打开状态(!Window.Closed),以及(B)是否具有所需的可用函数。

Simple values like strings are fine, but generally it isn't a good idea to pass complex objects such as functions, DOM elements and closures between windows. If a child window stores an object from its opener, then the opener closes, that object can become 'dead' (in some browsers such as IE), or cause a memory leak. Weird errors can ensue.

Yes browsers clear all ref. for a window. So you have to search a ClassName of something on the main window or use cookies as Javascript homemade ref.

I have a radio on my project page. And then you turn on for the radio it´s starts in a popup window and i controlling the main window links on the main page and show status of playing and in FF it´s easy but in MSIE not so Easy at all. But it can be done.

Passing variables between the windows (if your windows are on the same domain) can be easily done via:

  1. Cookies
  2. localStorage. Just make sure your browser supports localStorage, and do the variable maintenance right (add/delete/remove) to keep localStorage clean.

You can pass variables, and reference to things in the parent window quite easily:

// open an empty sample window:
var win = open("");
win.document.write("<html><body><head></head><input value='Trigger handler in other window!' type='button' id='button'></input></body></html>");


// attach to button in target window, and use a handler in this one:
var button = win.document.getElementById('button');


button.onclick = function() {
alert("I'm in the first frame!");
}

我一直在努力成功地将参数传递给新打开的窗口。
以下是我的想法:

function openWindow(path, callback /* , arg1 , arg2, ... */){
var args = Array.prototype.slice.call(arguments, 2); // retrieve the arguments
var w = window.open(path); // open the new window
w.addEventListener('load', afterLoadWindow.bind(w, args), false); // listen to the new window's load event
function afterLoadWindow(/* [arg1,arg2,...], loadEvent */){
callback.apply(this, arguments[0]); // execute the callbacks, passing the initial arguments (arguments[1] contains the load event)
}
}

示例呼叫:

openWindow("/contact",function(firstname, lastname){
this.alert("Hello "+firstname+" "+lastname);
}, "John", "Doe");

活生生的例子

Http://jsfiddle.net/rj6o0jzw/1/

您可以使用 window.name作为窗口之间的数据传输-它也可以跨域工作。虽然没有官方支持,但据我所知,跨浏览器工作得很好。

更多关于 Stackoverflow Post 的信息

可以从“父”窗口向“子”窗口传递消息:

在“父窗口”中打开子窗口

    var win = window.open(<window.location.href>, '_blank');
setTimeout(function(){
win.postMessage(SRFBfromEBNF,"*")
},1000);
win.focus();

根据上下文被替换

在“孩子”中

    window.addEventListener('message', function(event) {
if(event.srcElement.location.href==window.location.href){
/* do what you want with event.data */
}
});

If 测试必须根据上下文进行更改

对我来说,下面的方法不管用

var A = {foo:'bar'};
var w = window.open("http://example.com");
w.B = A;


// in new window
var B = window.opener.B;

但是这很有效(注意变量名)

var B = {foo:'bar'};
var w = window.open("http://example.com");
w.B = B;


// in new window
var B = window.opener.B;

变量 B 也应该是全局的。