Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass a JavaScript variable to another browser window?

I have a page which spawns a popup browser window. I have a JavaScript variable in the parent browser window and I would like to pass it to the popped-up browser window.

Is there a way to do this? I know this can be done across frames in the same browser window but I'm not sure if it can be done across browser windows.

like image 639
Tom Kidd Avatar asked Sep 17 '08 20:09

Tom Kidd


People also ask

How do you pass a variable to another page in JavaScript?

There are two ways to pass variables between web pages. The first method is to use sessionStorage, or localStorage. The second method is to use a query string with the URL.

How do you pass a value in HTML?

For sending data to two servelets make one button as a submit and the other as button. On first button send action in your form tag as normal, but on the other button call a JavaScript function here you have to submit a form with same field but to different servelets then write another form tag after first close.

What is window opener in JavaScript?

opener. The Window interface's opener property returns a reference to the window that opened the window, either with open() , or by navigating a link with a target attribute. In other words, if window A opens window B , B. opener returns A .


2 Answers

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.

like image 126
Victor Avatar answered Oct 07 '22 15:10

Victor


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.

like image 27
MarkR Avatar answered Oct 07 '22 16:10

MarkR