Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute javascript function in a another iframe when parent is from different domain

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?

like image 225
Frushko Avatar asked Jun 13 '10 15:06

Frushko


People also ask

Can an iframe contain another iframe?

After some research I found that you can't nest an iframe inside of an iframe in the same file. Instead what you need to do is add a reference to the child iframe as the src attribute for the parent iframe.

Can you run JavaScript in an iframe?

It is called an inline frame because to the user it is all one web page. The child iframe is a complete browsing environment within the parent frame. It can load its own JavaScript and CSS separate from the parent. They can also be refreshed and loaded asynchronously from the parent site.

Can I load an iframe from a different domain?

html and iframe. html), one for parent window and another one is for iframe. Both can be in same domain or in different domain.

How does iframe pass value to parent?

Sending some data from the child iframe to the parent window is also pretty simple. Whenever you embed an iframe, the iframe will have a reference to the parent window. You just need to use the PostMessage API to send data via the window. parent reference of the parent window.


1 Answers

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.

like image 54
Sean Kinsey Avatar answered Sep 20 '22 06:09

Sean Kinsey