简体   繁体   English

JavaScript:如何在window.opener上警告变量

[英]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. 上面的例子不起作用。

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/"); 测试用例:输入javascript:void window.open("http://stackoverflow.com/"); in this window. 在这个窗口中。 A new window will open. 将打开一个新窗口。
Type javascript:void window.opener.alert(location.href); 输入javascript:void window.opener.alert(location.href); in the location bar of the new window, and press Enter. 在新窗口的位置栏中,按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. 我已经在FireFox 3.6.22和最新版本的Chromium中成功执行了此操作。 When I open "http://www.example.com/" instead of "http://stackoverflow.com/", a JavaScript error will occur, caused by the same-origin policy. 当我打开“http://www.example.com/”而不是“http://stackoverflow.com/”时,将发生由同源策略引起的JavaScript错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM