Crossdomain function calling from iframe to parent page

If the iframe Page B and the top Page A share the same top-level domain,but not the second-level domain,you can simply add this line to your code in order to assign the calling accessment from Page B to A

document.domain = "top-level-domain.com";

If the iframe Page B is placed on a totally different domain,then what?Still there is a way.
Basically,I achieved this by refreshing the location(src) value of the other iframe.

1.Define a function for calling in Page A:

function waiting4Callback(_p:String)
{
	alert("hello:" + _p);
}

2.Iframe Page B on Page A:

<i_frame id="IFrame_PageB" marginWidth=0 marginHeight=0 src="web-address-of-Page-B" frameBorder=0 scrolling="no" width="1000" height="500"></i_frame>

If you try to call A’s function immediately in Page B,obviously it is illegal and maybe it will cause an Error on your broswer.

window.parent.waiting4Callback("hello A");

3.The solution is to create a Page C which will be iframed later by Page B,and add this javascript code in Page C:

window.parent.parent.waiting4Callback( window.location.hash.substring(1));

4.Place the proxy.html file together with Page A and ensure that they share exactly the same domain

5.Add this iframe node on Page B in order to iframe Page C:

<i_frame id="IFrame_PageC" name="IFrame_PageC" marginWidth=0 marginHeight=0 src="" frameBorder=0 width="0" scrolling=0 height="0"></i_frame>

6.Now you can call Page A’s function and send Param in Page B like this:

document.getElementById("IFrame_PageC").src = "http://address-of-page-c.html" + "?random_anti_cache="+Math.random()+"#"+"String param to be sent";

In step 6,the Page C is refeshed by Page B,so the command added in step 3 executes to help Page C receive the String param from the value of “window.location.hash”.
Meanwhile,”window.parent.parent” in Page C refers to Page A,with which Page C shares the same domain.They don’t have any problem in communicating with each other,so in the scope of Page C,callback can be successfully sent to Page A.

*Please replace “i_frame” with “iframe” in the html code above

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s