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?

2
  • use $localstorage or ngStorage in angularjs Commented Jun 10, 2016 at 9:19
  • Are you using both AngularJS and backbone.js? Commented Jun 10, 2016 at 15:07

2 Answers 2

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'
Sign up to request clarification or add additional context in comments.

Comments

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;

2 Comments

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.
See edits.. Although there is much more material available on how to extract specific data types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.