Alex
Alex

Reputation: 181

JavaScript: How to alert variable on window.opener

Opened Window:

 window.opener.variable = document.getElementById(target).value; //string
 window.opener.focus();

Window Opener

alert(variable);

Could this be done? The above example doesn't work.

Upvotes: 5

Views: 4911

Answers (1)

Rob W
Rob W

Reputation: 348992

This can be done, but not across different domains.

If you want to cause the original window to alert the variable:

window.opener.alert(variable);

Testcase: Type javascript:void window.open("http://stackoverflow.com/"); in this window. A new window will open.
Type javascript:void window.opener.alert(location.href); in the location bar of the new window, and press Enter. The original window will show an alert box.

I have successfully executed this in FireFox 3.6.22 and the newest version of Chromium. When I open "http://www.example.com/" instead of "http://stackoverflow.com/", a JavaScript error will occur, caused by the same-origin policy.

Upvotes: 3

Related Questions