47

How to acess parent window object using jquery?

This is my Parent window variable , I want to set its value after closing child window .

$('#serverMsg').html('some text here');
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Vicky
  • 9,515
  • 16
  • 71
  • 88

4 Answers4

78
window.opener.$("#serverMsg")
roman
  • 11,143
  • 1
  • 31
  • 42
18

If you are in a po-up and you want to access the opening window, use window.opener. The easiest would be if you could load JQuery in the parent window as well:

window.opener.$("#serverMsg").html // this uses JQuery in the parent window

or you could use plain old document.getElementById to get the element, and then extend it using the jquery in your child window. The following should work (I haven't tested it, though):

element = window.opener.document.getElementById("serverMsg");
element = $(element);

If you are in an iframe or frameset and want to access the parent frame, use window.parent instead of window.opener.

According to the Same Origin Policy, all this works effortlessly only if both the child and the parent window are in the same domain.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
11

or you can use another approach:

$( "#serverMsg", window.opener.document )
kajo
  • 5,631
  • 4
  • 31
  • 29
  • I prefer this approach because it doesn't rely on the opening window having jQuery - although it probably will have jQuery anyway. – Damian Powell Sep 28 '11 at 08:55
0

Here is a more literal answer (parent window as opposed to opener) to the original question that can be used within an iframe, assuming the domain name in the iframe matches that of the parent window:

window.parent.$("#serverMsg")
John Langford
  • 1,272
  • 2
  • 12
  • 15