简体   繁体   中英

How to get cross-domain communication to work in iframes?

I have an iframe-based online help system that has worked well for years. With IE8 it chokes on some of the javascripting that calls location.toString(). This same code works fine in IE6.

Specifically, the code is:

var iss = parent.left.location.toString();
var isInd = iss.indexOf("indexframe");

I get a "permission denied" error. I believe the problem is related to cross-domain communications, which I'm not sure I fully understand. The whole package runs locally using local HTML and javascript files. I'm not trying to have a frame in one domain control a frame in another domain. Or maybe I'm way off base in assuming this is the problem.

Could someone help me to understand what I need to do to work around this issue?

Typically when accessing the content of another iframe, i use something like this:

var f = document.getElementById('IdOfIFrame'), 
    d = f.contentDocument||f.contentWindow;
alert(d.location);

If the iFrame and the parent Document are in the same domain then you should not get that error. It suggests to me that the documents are in different domains.

If the Iframe is in www.mydomain.com and the document is in help.mydomain.com YOU WILL GET AN ERROR! The pages must think they are in the exact same domain.

In both documents you could add javascript the set the domain:

document.domain = "mydomain.com";

Javascript will allow you to drop into the host domain on both pages. This allows you to communicate accross the frames. Of course if the pages are in different HOST domains then this won't work and javascript will throw the error.

If you are indeed accessing 2 domains from your site, and you own both of them, you can create an xml file that specifies which domains should be allowed to share. See the spec document . This opt-in cross-site access is supported by more than just Adobe (MS Silverlight for one). Here is Silverlight's support spec .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM