views:

124

answers:

2

Webpage A is embedded in an iframe inside of webpage B. A and B are on two different domains and therefore the same origin policy prevents A from accessing properties of B like so;

location = window.top.location.href // emits a "Permission denied" error

Is there any other way for A to get B's url?

+1  A: 

No

David Dorward
So you can only use window.top if your page is embedded in a page on the same domain?
jessehattabaugh
No, you can use window.top — you just can't read data out of the top frame. You can, for example, **set** location to get the browser to load a different URL into the 'top' frame.
David Dorward
A: 

If you have control over both domains, you can try a cross-domain scripting library like EasyXDM, which wrap cross-browser quirks and provide an easy-to-use API for communicating in client script between different domains using the best available mechanism for that browser (e.g. postMessage if available, other mechanisms if not).

Caveat: you need to have control over both domains in order to make it work (where "control" means you can place static files on both of them). But you don't need any server-side code changes.

In your case, you'd add javascript into one or both of your pages to look at the location.href, and you'd use the library to call from script in one page into script in the other.

Another Caveat: there are security implications here-- make sure you trust the other domain's script!

Justin Grant