admin管理员组

文章数量:1302406

I have an html page where I have this layout essentially:

<html>
<body>

<!-- iFrame A -->
<iframe></iframe>

<!-- iFrame B -->
<iframe></iframe>
</body>
</html>

In IFRAME "B", I'd like to call a js function in IFRAME "A", which will ultimately change the window.location property and redirect the page. I have jquery at my disposal as well, but have been unable to figure out a way of calling something in that adjacent frame.

Any suggestions?

I have an html page where I have this layout essentially:

<html>
<body>

<!-- iFrame A -->
<iframe></iframe>

<!-- iFrame B -->
<iframe></iframe>
</body>
</html>

In IFRAME "B", I'd like to call a js function in IFRAME "A", which will ultimately change the window.location property and redirect the page. I have jquery at my disposal as well, but have been unable to figure out a way of calling something in that adjacent frame.

Any suggestions?

Share asked Sep 11, 2010 at 23:56 mirezusmirezus 14.3k11 gold badges39 silver badges42 bronze badges 2
  • Are all 3 on the same domain? If not, which are on each domain, and which do you control? – Matthew Flaschen Commented Sep 11, 2010 at 23:58
  • Yep, all three on same domain – mirezus Commented Sep 12, 2010 at 14:21
Add a ment  | 

3 Answers 3

Reset to default 7

Assuming everything is on the same domain, and you have two iframes with ids "iframe-a" and "iframe-b", with jQuery in the parent:

In frame A:

function foo() {
  alert("foo from frame A");
}

From frame b:

window.parent.$("#iframe-a")[0].contentWindow.foo();

And you should see "foo from frame A" get alerted.

In some browsers, the window.frames array is only populated if the frames are named, rather than having only an ID If the frames are named and the content are from the same origin (domain, port, protocol), then window.frameb.functionName() will trigger the function in standard javascript. See other answer(s) for jQuery version

Use window.parent to get the parent (the main window) then use window.frames on the parent window to get frame B. From there call your function.

本文标签: jqueryHow to call javascript function inside an adjacent iframeStack Overflow