Can I pass a JavaScript variable to another browser window?

I have a page in which a browser popup appears. I have a JavaScript variable in the parent browser window, and I would like to pass it to the browser window that appears.

Is there any way to do this? I know that this can be done through frames in the same browser window, but I'm not sure if this can be done through browser windows.

+61
javascript browser
Sep 17 '08 at 20:35
source share
13 answers

If the windows are from one security domain, and you have a link to another window, yes.

The javascript open () method returns a link to the created window (or an existing window if it reuses an existing one). Each window created in this way receives a property, "window.opener" applied to it, pointing to its created window.

You can also use the DOM (depending on security) to access the properties of another, or its documents, frames, etc.

+38
Sep 17 '08 at 20:38
source share

Entering the code into the case, you can do this from the parent window:

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

or this is from a new window:

 var myVariable = window.opener.thisIsAnObject; 

I prefer the latter because you will probably have to wait for a new page to load anyway, so that you can access your items or whatever you need.

+85
Sep 17 '08 at 21:14
source share

Yes, scripts can access the properties of other windows in the same domain in which they have a handle (usually obtained through window.open/opener and window.frames / parent). Usually more manageable is calling functions defined in another window, rather than a fiddle with variables directly.

However, windows can die or move on, and browsers deal with it differently when they do it. Make sure that window (a) is still open (! Window.closed) and (b) has the function that you expect to receive before trying to call it.

Simple values, such as strings, are accurate, but it is generally not recommended to pass complex objects such as functions, DOM elements, and closures between windows. If the child window stores an object from its opener, then the opener closes, this object may become "dead" (in some browsers, such as IE), or cause a memory leak. Strange errors may occur.

+7
Sep 17 '08 at 21:17
source share

Transferring variables between windows (if your windows are in the same domain) can be easily done with:

  • Cookies
  • LocalStorage Just make sure your browser supports localStorage, as well as the right to service variables (add / remove / delete) to keep localStorage clean.
+3
May 23 '13 at 16:40
source share

In the 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 are potentially many mistakes you should make when analyzing key / value pairs, but I do not include it here. Perhaps in a later answer, someone might provide a more inclusive JSQL query processing the procedure.

+2
Sep 17 '08 at 21:05
source share

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

 // 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!"); } 
+2
Jan 31 '14 at 15:34
source share

You can pass the message from the parent window to the "child" window:

in the "parent window" open the child

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

replaceable according to context

In the "child"

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

If test should be changed according to context

+2
Jan 17 '17 at 21:30
source share

Yes, this can be done if both windows are in the same domain. The window.open () function will return the handle to a new window. A child window can access the parent window using the opener DOM element.

+1
Sep 17 '08 at 20:39
source share

Alternatively, you can add it to the URL and let the script (PHP, Perl, ASP, Python, Ruby, independently) process it from the other side. Something like:

 var x = 10; window.open('mypage.php?x='+x); 
0
Sep 17 '08 at 21:04
source share

I struggled to successfully pass arguments into the newly opened window.
Here is what I came up with:

 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 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) } } 

Call example:

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

Living example

http://jsfiddle.net/rj6o0jzw/1/

0
Nov 24 '15 at 13:49
source share

You can use window.name as a transfer of data between windows, as well as work with a cross domain. Not officially supported, but, in my opinion, it actually works very well in the browser.

Read more here in this Stackoverflow post

0
May 11 '16 at 17:48
source share

Yes, browsers clear all links. for the window. So you need to look for ClassName something in the main window or use cookies as Javascript source code.

I have a radio on my project page. And then you turn on for the radio that it starts in a pop-up window, and I control the links of the main window on the main page and show the playback status, and in FF it’s easy, but in MSIE it’s not so simple. But it can be done.

-one
08 Oct '11 at 9:50 a.m.
source share

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

-2
Sep 17 '08 at 20:37
source share



All Articles