views:

182

answers:

2

any simple examples i want ? thanks in advance.

A: 
window.opener.jQuery(myVariable);
cxfx
thanks for ur reply. i think "myVariable" is function name, if i correct, how to pass variable to it.If it is variable how to call my function?
Kumar
I'd imagine window.opener.$(myVariable) would work too.
cxfx
maybe you need window.opener.myFunction(someVariable)
cxfx
exactly, but how to define "myFunction(somevariable)" in parent window jquery.
Kumar
function myFunction(somevariable) { /* like this? */ }
cxfx
Fine, i tried, but in the function i wrote some jquery line. that is not working, so i want to know when parent window onload completes, how to find that is the question now?
Kumar
+1  A: 

opener is a reference to the window object of the opening document. I.e. you have access to the global javascript namespace of the opening window.

e.g.

<html>
  <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"&gt;&lt;/script&gt;
    <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 press "propagate url" in a (in any) popup window the function call will bubble up to the first non-popup window (having opener=null).

edit: keep in mind that security restrictions (like 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"&gt;&lt;/script&gt;
    <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>
VolkerK
Thank you. just add "opener.history.go(0);" before "opener.foo(url);" , now it is not working ( for me), any suggestions.
Kumar
"go(0)" Do you want to reload the (same) page?
VolkerK
yes, i am changing some of the main-window content in the popup, so i want it to reload with edited contents, and used history.go(0) for restoring the same page view as before.
Kumar
I think i may need when the main window reloading completes,so that i can call the jquery in it(main window). i tried with window.parent.onload=function_name; but it did't worked. any other way?
Kumar
Fine, after "context.history.go(0)" i want to execute some function. like "opener.foo(url);" . that fn. not working.after reloads. any ideas to check the completion of parent window reloading.
Kumar
Now that's an interesting question. I somehow doubt you can use $(opener.document).ready() for that, though I haven't tested it. If it doesn't work you should open another question on _that_ specific matter.
VolkerK
@VolkerK Thanks.
Kumar