0

I need to access the parent window of an iframe, this script is also loaded in the iframe window as well.. So basically I have this markup.

BODY | IFRAME | BODY | SCRIPT

$(window.parent.document).resize(resizeModal);

So I need once inside the modal window, for the fundition "resizeModal()" to run once it's parent window is resized, but it doesn't work :(

Any ideas?

Zong
  • 6,160
  • 5
  • 32
  • 46
Shannon
  • 570
  • 1
  • 6
  • 14
  • 2
    I don't believe scripts inside of an iframe can affect the page they're embedded in. – Charlie Sep 01 '12 at 03:58
  • IMO, an iframe's contents should not be responsible for resizing the window. Besides, what if (completely hypothetical), what if this "parent window" is in an iframe of another page? – Tyler Crompton Sep 01 '12 at 05:00

2 Answers2

0

I originally assumed that you could not have a script inside of an iframe affect the parent page, but it looks like you can:

Can javascript running inside an iframe affect the main page?

You share variables between the iframe and the parent window by means of

window.whatever = "value";

or the parent. property.

Community
  • 1
  • 1
Charlie
  • 11,380
  • 19
  • 83
  • 138
0

Try this:

$("#element", top.document); 

The top.document tells the selector to target the myid element which exists in the topmost document (your parent page). In order for this to work, jquery must be loaded in the file which is viewed through the iframe.

But why not do it the other way? The parent window be listening to the event to resize and send the event to the iframe?

$(window).resize(function () { 
    $("#iframe").content().find("#anything");
});
Shankar Cabus
  • 9,302
  • 7
  • 33
  • 43