开发者

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 p开发者_JAVA百科roblem 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?


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.


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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜