Quickest way to pass data to a popup window I created using window.open()?

Viewed 42957

I have javascript code to open a popup window using the window.open() function. I'd like to pass data from the parent window to that popup window and I can't seem to find an elegant or simple solution anywhere on google or SO, but it seems like there should be support for this built into JS. What's the easiest way to pass data to the popup window?

Thanks in advance for all your help!

6 Answers

You could also set the variable as the 'name' attribute in open.window function and then use window.name in the new window to retrieve the value.

There has some ways:

  1. Use window.localStorage, set the data in origin window and get the data in the popup window.
  2. Attach to the URL as query param.
  3. Doug Neiner mentioned above:
// Store the return of the `open` command in a variable
var newWindow = window.open('http://www.example.com');

// Access it using its variable
newWindow.my_special_setting = "Hello World";

// In the child (popup) window, you could access that variable like this:
window.my_special_setting

In the parent use:

let newWindow = window.open('http://www.example.com');
newWindow["myVar"] = "Hello World"

In the Child use: window["myVar"] to access it.

Related