javascript window.opener 在 Firefox 和 chrome 中不起作用,但在 IE 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25359470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.opener doesn't work in firefox and chrome but in IE
提问by maxschr
I have a problem with the window.opener function (JS):
我的 window.opener 函数(JS)有问题:
I created 3 simple pages to simulate my problem:
我创建了 3 个简单的页面来模拟我的问题:
test.php:
测试.php:
<html>
<head>
</head>
<body>
<input type="button" onclick="window.open('first_page_in_popup.php', 'Popup', 'width=470,height=500');">
<input type="text" id="content" name="content" >
</body>
</html>
first_page_in_popup.php:
first_page_in_popup.php:
<html>
<head>
<script type="text/javascript" >
window.opener.document.getElementById('content').value= "Test from first page in popup";
window.open('second_page_in_popup.php', 'Popup');
</script>
</head>
<body>
</body>
</html>
second_page_in_popup.php:
second_page_in_popup.php:
<html>
<head>
<script type="text/javascript" >
window.opener.document.getElementById('content').value= "Test from second page in popup";
</script>
</head>
<body>
</body>
</html>
Result: In IE, the input field has the content "Test from second page in popup". In Chrome and Firefox, the content is : "Test from first page in popup".
结果:在 IE 中,输入字段的内容为“从弹出窗口中的第二页测试”。在 Chrome 和 Firefox 中,内容是:“从弹出窗口的第一页进行测试”。
The Error Message:
错误信息:
window.opener.document.getElementById(...) is null
回答by maxschr
I solved the problem by changing the code of page "first_page_in_popup.php" to:
我通过将页面“first_page_in_popup.php”的代码更改为:
<html>
<head>
<script type="text/javascript" >
window.opener.document.getElementById('content').value= "Test from first page in popup";
window.location.href = 'second_page_in_popup.php';
</script>
</head>
<body>
<p> First Page </p>
<input type="text" id="first" name="first">
</body>
</html>
回答by Syed Ali Taqi
try parent.window.opener
instead of window.opener
尝试parent.window.opener
代替window.opener
回答by RahulKT
window.opener.document.getElementById(...) is null
that means content in window.opener.document.getElementById is null or undefined please try to define
这意味着 window.opener.document.getElementById 中的内容为空或未定义请尝试定义
content in second php as defined in first php
hope this will work.
希望这会奏效。