0

I have opened a separate window from a page using window.open(url);.I have set some global variables inside the first window. Now I want to access these variables inside my new window. How can I do it? or is there any way to achieve this?

Shubham Agrawal
  • 298
  • 4
  • 10

2 Answers2

4

Yes, it is possible via the window.opener object.

For example: If you have this code in an HTML file:

// JavaScript code in index.html file
window.myVariable = 'variable value';
window.open('child.html');

The myVariable variable is accessible in the child.html file

// JavaScript code in child.html file
console.log(window.opener.myVariable); // outputs 'variable value'
Tasos K.
  • 7,979
  • 7
  • 39
  • 63
1

You can use Querystrings to achieve this. There are a number of links available with valuable resources if you do so:

how to exchange variables between two HTML pages?

Pass JavaScript variable between two HTML pages

Passing JavaScript data values between 2 HTML pages.

A basic usage example is the following:

Screen1.html (passing data from):

function sendData() 
{      
  window.location = "Screen2.html?" + dataToBeSent;

}

Screen2.html (receiving data):

var query = window.location.search;
Community
  • 1
  • 1
Sandman
  • 2,247
  • 1
  • 13
  • 26
  • Please explain how to use `Querystrings` in the answer itself. Without it this is just a bunch of external links and not really an answer. – T J Jun 10 '16 at 15:10
  • See edits.. Although there is much more material available on how to extract specific data types. – Sandman Jun 10 '16 at 16:06