Peter Kota
Peter Kota

Reputation: 8340

Get browser URL from two level depth iframe

There is an iframe in an other iframe. I would like to get the browser's URL (in example: http://test) from the second iframe.

//browser URL: http://test <-- I need this URL

<iframe src="http://level1">
    <iframe src="http://level2">
        <!-- some content -->
    </iframe>
</iframe>

I tried document.referrer but it gives back the first iframe's URL:

<iframe src="http://level1">
    <iframe src="http://level2">
        console.log(document.referrer); //It gives back http://level1
    </iframe>
</iframe>

UPDATE:

The top.location.href doesn't work because there are different domains. Different the browser, the first and the second iframe domain. (our website - adserver - third-party website)

Upvotes: 1

Views: 1354

Answers (1)

dekajoo
dekajoo

Reputation: 2102

As said it is in general not possible from cross-domain iframes, but in the case of Chrome you can use window.location.ancestorOrigins that will give you the "top level url" and a list of all the domains if you are in several iframes no matter if they are cross-domain or not.

Upvotes: 3

Related Questions