JavaScript window.opener 调用父函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10591050/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:01:21  来源:igfitidea点击:

JavaScript window.opener call parent function

javascriptwindow.opener

提问by RPIBuckHunter

I am trying to call a javascript function defined in a parent from a child window. I have two files like this:

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

Parent:

家长:

<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>

And 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>

I can not, for the life of me, call the function foo from the child process. I thought this should be possible with the window.opener object, but I can not seem to make this work. Any suggestions?

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

回答by Joseph

Ensure you are accessing this via http:// so the Same origin policy passes and you can access opener from the child. It won't work if you're just using file://.

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

回答by joutsen

Answering Rahul's question:

回答拉胡尔的问题:

Every browser can load pages from server or from local filesystem. To load file from local filesystem you should put to the browser the address like this file://[path], where [path] is the absolute path to the file in filesystem (including drive letter on Windows, see http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspxfor details).

每个浏览器都可以从服务器或本地文件系统加载页面。要从本地文件系统加载文件,您应该将这样的地址放入浏览器file://[path],其中 [path] 是文件系统中文件的绝对路径(包括 Windows 上的驱动器号,请参阅http://blogs.msdn.com/b/ ie/archive/2006/12/06/file-uris-in-windows.aspx了解详情)。

To load file from local HTTP server (if you have one) you should put to address something like this http://localhost:[port]/[path], where [port] is the port where your server is running (default is 80) and [path] is the path of the file relative to the server's document root folder. Document root folder depends on the server configuration.

要从本地 HTTP 服务器(如果有的话)加载文件,你应该把地址放在这样的地方http://localhost:[port]/[path],其中 [port] 是你的服务器运行的端口(默认是 80),[path] 是文件的相对路径到服务器的文档根文件夹。文档根文件夹取决于服务器配置。

So, as you see, the same local file can be loaded to the browser in two ways. There is however big difference between these two ways. In the first case the browser doesn't use HTTP protocol to load the file and therefore is missing many things necessary for different mechanisms to work properly. For example AJAX doesn't work with local files, as HTTP response status is not 200, etc.

因此,如您所见,可以通过两种方式将相同的本地文件加载到浏览器。然而,这两种方式之间存在很大差异。在第一种情况下,浏览器不使用 HTTP 协议来加载文件,因此缺少不同机制正常工作所需的许多东西。例如,AJAX 不适用于本地文件,因为 HTTP 响应状态不是 200 等。

In this particular example the browser security mechanism didn't get the origin information and was preventing from accessing the parent window.

在这个特定的例子中,浏览器安全机制没有获得源信息,并阻止访问父窗口。