Skip to content
Advertisement

Get current URL from IFRAME

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement