Thomson
Thomson

Reputation: 21645

How to change the domain of current document?

I have an iframe in a document from a different domain. It seems the browser blocks me to access the inner iframe for this reason. Can I change both domain to the same value? I tried document.domain = 'targetdomain', but there is DOM exception thrown out.

Upvotes: 0

Views: 239

Answers (1)

jfriend00
jfriend00

Reputation: 707696

The domain of the iframe is the domain of the URL that loaded into the iframe.

You can change the iframe.src to a different document if you like on a different domain, but you cannot change the domain of the iframe itself while keeping the same document loaded in order to enable you to access the content as that would be the very security violation that the same-origin policy protects from.

The one exception to changing the domain is when using sub-domains. You can read about that here.

So, unless one of these two domains is a sub-domain of the other, you cannot access one from the other.

In the latest browsers, there is an HTML5 message passing scheme that allows two cooperating frames/windows from different domains to exchange messages with each other. The key here is that both frames/windows must cooperate for this to happen (meaning you must have code control over both frames/windows). You can read more about that here.

Upvotes: 4

Related Questions