Get current URL from IFRAME

chris picture chris · Jun 2, 2009 · Viewed 195.5k times · Source

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.

Answer

kkyy picture kkyy · Jun 2, 2009

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.