6

The page A.com has 2 iframes B.com/page1 and B.com/page2. This is the code of A.com:

<html><body>
    <iframe src="b.com/page1" name="iframe1" id="iframe1">
    <iframe src="b.com/page2">
</body></html>

I want to execute js function on B.com/page1 from B.com/page2. Both examples below works well when the parent is from the same domain but not in cross domain scenario:

parent.window.frames['iframe1'].SomeFunction(args);

or

parent.document.getElementById('iframe1').contentWindow.SomeFunction(args);

Is there any way to do it?

4 Answers 4

8

Instead of

parent.window.frames['iframe1'].SomeFunction(args);

use

parent.frames['iframe1'].SomeFunction(args);

You are allowed to traverse the frames collection of any window that you can reference, but in this case you are trying to traverse the recursive window property of parent (which IS the window). This is not allowed.

Sign up to request clarification or add additional context in comments.

1 Comment

Sean , but window.window is exactly like window. However- I tried this parent.window.window.window.frames['if1'].window.window.window.s2(1) and it did work...(or do you talk about traversing something else ?)
2

No, the browser doesn't allow interacts between iframes that are not on the same domain at all.

3 Comments

Just to make it clear. Although the 2 iframes are from the same domain there is no direct way for them to interact because the parent is from different domain?
No, because the iframes can only "talk to" each other through the parent frame, and the frames can't access the parent frame from a different domain.
Actually, this is incorrect. He only needs to change parent.window.frames into parent.frames
0

As Aaron have already told you, browsers don't allow this, but there are ways around it. You'll need to create a small hack. Eugene Gladyshev has a post on it on his blog.

Comments

0

As Aaron states, it is not allowed to interact between iframes, but you should take a look at easyXDM, it helps with cross domain communication in javascript:

http://easyxdm.net/wp/

Here is the example you might be looking for:

http://easyxdm.net/wp/2010/03/17/sending-and-receiving-messages/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.