简体   繁体   English

JavaScript window.opener调用父函数

[英]JavaScript window.opener call parent function

I am trying to call a javascript function defined in a parent from a child window. 我试图从子窗口调用父类中定义的javascript函数。 I have two files like this: 我有两个这样的文件:

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. 在我的生活中,我不能从子进程中调用函数foo。 I thought this should be possible with the window.opener object, but I can not seem to make this work. 我认为这应该可以使用window.opener对象,但我似乎无法使这个工作。 Any suggestions? 有什么建议?

Ensure you are accessing this via http:// so the Same origin policy passes and you can access opener from the child. 确保您通过http://访问此内容,以便通过相同的原始策略,您可以从子项访问opener。 It won't work if you're just using file://. 如果您只是使用file://,它将无法工作。

Answering Rahul's question: 回答Rahul的问题:

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.aspx for 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. 要从本地HTTP服务器(如果有的话)加载文件,你应该设置类似这样的http://localhost:[port]/[path] ,其中[port]是服务器运行的端口(默认为80)和[path]是文件相对于服务器文档根文件夹的路径。 Document root folder depends on the server configuration. 文档根文件夹取决于服务器配置。

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. 在第一种情况下,浏览器不使用HTTP协议来加载文件,因此缺少许多不同机制正常工作所必需的东西。 For example AJAX doesn't work with local files, as HTTP response status is not 200, etc. 例如,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. 在此特定示例中,浏览器安全机制未获取原始信息并且无法访问父窗口。

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

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