user70192
user70192

Reputation: 14204

JQuery - Write to opener window

I have an HTML page that opens another page via JavaScript. When a user clicks a button in the other page, I want to post a message in a DIV of the opening page via JQuery. I cannot put my finger on it, but I cannot seem to get this to work. Here is my opener page

<html>
  <head>
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
  </head>

  <body>
    <input type="button" onclick="window.open('dialog.html', '_blank', 'height=200, width=300');" value="launch!" />
    <div id="testDiv"></div>
  </body>
</html>

When the user clicks the "launch!" button, a dialog will appear. The code for the dialog looks like this:

<html>
  <head>
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
  </head>

  <body>
    <input type="button" onclick="updateOpener()" value="Update Opener" />
    <script type="text/javascript">
      function updateOpener()
      {
        var testDiv = window.opener.jQuery("#testDiv");
        if (testDiv != null) {
      alert("here");
      testDiv.html("Updated!");
        }
      }
    </script>
  </body>
</html>

Surprisingly, the alert box appears. However, I cannot seem to update the HTML of the DIV in my opening page. Does anyone know how to do this?

Upvotes: 8

Views: 21420

Answers (4)

Josh Stodola
Josh Stodola

Reputation: 82513

You can't do that if the parent page (the opener) resides on another domain. Otherwise, your code works perfectly.

Also, your != null check is probably not doing what you think it is doing, as the jQuery function never returns null. If you are checking for the existence of an element, you need to do it this way...

var el = $("#myElementId");
if(el.length == 0)
  alert('Not found!');

Upvotes: 2

Nick Riggs
Nick Riggs

Reputation: 1275

Oddly, your example works fine for me in Chrome, IE 8 and FireFox. Do you have any other details?

Upvotes: 0

eimaj
eimaj

Reputation: 655

Ummm, it works for me in Firefox 3.0.11, IE8 and Chrome 2... (I.e. the dialog.html button updates the HTML in the opener page to say 'Updated!'.)

Upvotes: 0

Sev
Sev

Reputation: 15717

You're referencing "confirmDiv". Where is that DIV?

Upvotes: 5

Related Questions