How do I implement Cross Domain URL Access from an Iframe using Javascript?

Sri Kumar picture Sri Kumar · Sep 4, 2009 · Viewed 101.3k times · Source

I need to access the Parent Domain URL from my Iframe which is in another domain.

For example, "example.com" is my website which has an Iframe from another parent domain, such as "google.com". Here I need to access the parent domain URL from my example.com. That is, I need to get the URL "google.com" in my "example.com" domain. Moreover, the Parent domain cannot be hard coded.

What I tried was using the following code:

window.parent.location.href()

but this results in Access denied error. How do I implement this properly in order to achieve this?

Answer

Yuliy picture Yuliy · Jun 22, 2010

You can try and check for the referer, which should be the parent site if you're an iframe

you can do that like this:

var href = document.referrer;