Javascript – How to implement Cross Domain URL Access from an Iframe using Javascript

cross-domainiframejavascript

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?

Best Answer

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;