122

Is there a simple way to get the current URL from an iframe?

The viewer would going through multiple sites. I'm guessing I would be using something in javascript.

chris
  • 20,791
  • 29
  • 77
  • 90
  • 4
    If you came here looking for the URL of the "parent" page of the iframe, see http://stackoverflow.com/q/3420004/32453 – rogerdpack Nov 08 '16 at 20:48
  • As a browser user, how do I check the url of a webpage in an IFRAME. Once there was a `property` in the right click menu, but it was gotten rid of by the browser. – Zhang May 22 '23 at 09:54

9 Answers9

206

For security reasons, you can only get the url for as long as the contents of the iframe, and the referencing javascript, are served from the same domain. As long as that is true, something like this will work:

document.getElementById("iframe_id").contentWindow.location.href

If the two domains are mismatched, you'll run into cross site reference scripting security restrictions.

See also answers to a similar question.

Community
  • 1
  • 1
kkyy
  • 12,214
  • 3
  • 32
  • 27
  • There is no winning answer in the question I linked... if you mean the topmost answer, it isn't really a working solution. With IE, you could try the HTA approach provided also in the answers to the aforementioned question. – kkyy Jun 03 '09 at 09:12
  • What if we add the attribute `sandbox="allow-same-origin"` to the iFrame? – Roel Apr 11 '16 at 09:32
  • And if we allow the new source on the Iframe's target server, would we run into a problem? – Joe Yahchouchi May 18 '17 at 18:43
  • Another way: `window.frames[0].location.href` – Shayan Dec 08 '19 at 13:59
  • Simplest: `window.top.location.href` https://developer.mozilla.org/en-US/docs/Web/API/Window/top – Alex Aug 25 '22 at 20:33
22

If your iframe is from another domain, (cross domain), the other answers are not going to help you... you will simply need to use this:

var currentUrl = document.referrer;

and - here you've got the main url!

ParPar
  • 7,355
  • 7
  • 43
  • 56
4

If you are in the iframe context,

you could do

const currentIframeHref = new URL(document.location.href);
const urlOrigin = currentIframeHref.origin;
const urlFilePath = decodeURIComponent(currentIframeHref.pathname);

If you are in the parent window/frame, then you can use https://stackoverflow.com/a/938195/2305243 's answer, which is

document.getElementById("iframe_id").contentWindow.location.href
Alan Dong
  • 3,981
  • 38
  • 35
4

I had an issue with blob url hrefs. So, with a reference to the iframe, I just produced an url from the iframe's src attribute:

    const iframeReference = document.getElementById("iframe_id");
    const iframeUrl = iframeReference ? new URL(iframeReference.src) : undefined;
    if (iframeUrl) {
        console.log("Voila: " + iframeUrl);
    } else {
        console.warn("iframe with id iframe_id not found");
    }
Rob
  • 5,534
  • 1
  • 22
  • 22
3

If you're inside an iframe that don't have cross domain src, or src is empty:

Then:

function getOriginUrl() {
    var href = document.location.href;
    var referrer = document.referrer;
    // Check if window.frameElement not null
    if(window.frameElement) {
        href = window.frameElement.ownerDocument.location.href;
        // This one will be origin
        if(window.frameElement.ownerDocument.referrer != "") {
            referrer = window.frameElement.ownerDocument.referrer;
        }
    }
    // Compare if href not equal to referrer
    if(href != referrer) {
        // Take referrer as origin
        return referrer;
    } else {
        // Take href
        return href
    }
}

If you're inside an iframe with cross domain src:

Then:

function getOriginUrl() {
    var href = document.location.href;
    var referrer = document.referrer;
    // Detect if you're inside an iframe
    if(window.parent != window) {
        // Take referrer as origin
        return referrer;
    } else {
        // Take href
        return href;
    }
}
2

Hope this will help some how in your case, I suffered with the exact same problem, and just used localstorage to share the data between parent window and iframe. So in parent window you can:

localStorage.setItem("url", myUrl);

And in code where iframe source is just get this data from localstorage:

localStorage.getItem('url');

Saved me a lot of time. As far as i can see the only condition is access to the parent page code. Hope this will help someone.

Beny
  • 157
  • 9
  • 9
    local storage works only on the same domain... so why going with all of this trouble? simply query the location.href – Yuki Sep 14 '18 at 14:22
0

I've been trying to retrieve the full URL from the inside of the shopify google analytics additional script iframe. document.refferer shows only hostname without search params. But I found another property that worked for me document.baseURI

Crashh
  • 9
  • 4
-1

Some additional information for anyone who might be struggling with this:

You'll be getting null values if you're trying to get URL from iframe before it's loaded. I solved this problem by creating the whole iframe in javascript and getting the values I needed with the onLoad function:

var iframe = document.createElement('iframe');
iframe.onload = function() {

    //some custom settings
    this.width=screen.width;this.height=screen.height; this.passing=0; this.frameBorder="0";

    var href = iframe.contentWindow.location.href;
    var origin = iframe.contentWindow.location.origin;
    var url = iframe.contentWindow.location.url;
    var path = iframe.contentWindow.location.pathname;

    console.log("href: ", href)
    console.log("origin: ", origin)
    console.log("path: ", path)
    console.log("url: ", url)
};

iframe.src = 'http://localhost/folder/index.html';

document.body.appendChild(iframe);

Because of the same-origin policy, I had problems when accessing "cross origin" frames - I solved that by running a webserver locally instead of running all the files directly from my disk. In order for all of this to work, you need to be accessing the iframe with the same protocol, hostname and port as the origin. Not sure which of these was/were missing when running all files from my disk.

Also, more on location objects: https://www.w3schools.com/JSREF/obj_location.asp

GChuf
  • 1,135
  • 1
  • 17
  • 28
-1

Not a simple way, but not particularly difficult either. Execute document.documentElement.outerHTML and you'll get the complete source of the "surrounding" page. From there it is easy to use a regular expression to search for the URL of the iframe.

d-b
  • 695
  • 3
  • 14
  • 43
  • 1
    The would give the value of the `src` attribute, which is the *initial* URL, not the *current* URL. – Quentin Jul 14 '23 at 06:24
  • @Quentin You are right, but why does it seem to be blocked to get the initial URL using more regular methods (class, ID) when it is easy to access it this way? – d-b Jul 14 '23 at 06:27
  • It isn't. https://jsbin.com/mopohugica/1/edit?html,js,output – Quentin Jul 14 '23 at 06:35