How can I call the jquery function of the parent window with a variable from a javascript popup?

How can I call the jquery function of the parent window with a variable from a javascript popup? Any simple examples I can look at?

+4
source share
3 answers

opener is a reference to the window object of an open document. That is, you have access to the global javascript namespace for the open window.

eg.

 <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script> <script type="text/javascript"> function foo(url) { // if there is a reference to the opening window if (null!=opener) { // we call the function in the context of the opening window opener.foo(url); } else { // otherwise show the data $('#d1').html(new Date() + " : " + url); } } </script> </head> <body> <div id="d1">...</div> <button onclick="window.open('?');">new window</button> <button onclick="foo(document.URL);">propagte url</button> </body> </html> 

if you click "spread url" in the popup window (in any), the function call will go to the first non-popup window (with opener = null).

edit: keep in mind that security restrictions (for example, cross-domain checks) implemented in the browser apply.

edit2: example with history.go (0)

 <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script> <script type="text/javascript"> function foo() { var context = (null!=opener) ? opener : window; context.history.go(0); } $(document).ready( function() { $('#d1').html("document ready at "+ new Date()); }); </script> </head> <body> <div id="d1">...</div> <button onclick="window.open('?');">new window</button> <button onclick="foo(document.URL);">...and action</button> </body> </html> 
+5
source

Usually you define a plugin function as follows:

 (function($) { // jquery no conflict $.fn.myPlugin = function(myvar){ // do something } })(jQuery); 

Then use this:

 window.opener.$.fn.myPlugin(myvar); 
+2
source
 window.opener.jQuery(myVariable); 
0
source

All Articles