Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript window.opener call parent function

I am trying to call a javascript function defined in a parent from a child window. 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. I thought this should be possible with the window.opener object, but I can not seem to make this work. Any suggestions?

like image 838
RPIBuckHunter Avatar asked May 14 '12 21:05

RPIBuckHunter


People also ask

How to call a function in parent from iframe?

Click on the button to call parent function. When you click on the button, onClick event will be fired. onClick event will call function from parent window by using parent variable. By using parent variable you can call function from parent window.

How to call parent function in jQuery?

CallParent(); window. parent holds a reference to the parent of the current window or subframe. If a window does not have a parent, its parent property is a reference to itself.

How do you call an iframe function?

To call JavaScript function in an iframe, we get the window object of the iframe with the contentWindow property. document. getElementById("resultFrame"). contentWindow.


2 Answers

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://.

like image 152
Joseph Avatar answered Oct 26 '22 12:10

Joseph


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.aspx for details).

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.

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.

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

like image 44
joutsen Avatar answered Oct 26 '22 12:10

joutsen