Search code examples
javascriptwindowbrowser-tab

Why am I unable to reach parent window's variables?


Let's suppose I open a web-page and run this Javascript:

var test = "Success";
window.open(window.location.href);

Then, in the opened window

console.log(window.parent.test);

will yield undefined. I can solve the problem by doing this:

var test = "Success";
window.open(window.location.href).myParent = window;

and then, in the other window

console.log(test);

will yield "Success". So, the problem can be solved, but I wonder why is the opened window unable to reach the parent window's variables out of the box?


Solution

  • Have you tried window.opener.test ?

    or specifically: console.log(window.opener.test);

    This link explains the difference between window.parent and window.opener:When to use window.opener / window.parent / window.top