gpt4 book ai didi

JavaScript window.opener 调用父函数

转载 作者:可可西里 更新时间:2023-11-01 01:32:01 24 4
gpt4 key购买 nike

我正在尝试从子窗口调用在父窗口中定义的 javascript 函数。我有两个这样的文件:

家长:

<html>
<head>
<title>Test</title>
<script type="text/javascript">
function foo () {
alert ("Hello from parent!");
}
function doStuff () {
var w = window.open("testa.html");
}
</script>
</head>
<body>
<input type="button" value="open" onClick="doStuff();" />
</body>
</html>

还有 child :

<html>
<head>
<title>Test A</title>
<script type="text/javascript">
function get() {
window.opener.foo();
}
</script>
</head>
<body>
<input type="button" value="Call Parent" onClick="get();" />
</body>
</html>

我这辈子都不能从子进程中调用函数 foo。我认为 window.opener 对象应该可以做到这一点,但我似乎无法做到这一点。有什么建议吗?

最佳答案

确保您通过 http://访问它,以便同源策略通过,您可以从 child 访问 opener。如果您只是使用 file://,它将不起作用。

关于JavaScript window.opener 调用父函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10591050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com