Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 644
chris Avatar asked Jun 02 '09 06:06

chris


People also ask

How do I get the current URL of an iframe?

To get current URL from an iframe with JavaScript, we can use the contentWindow. location. href property. Then we get the current URL of the iframe with the contentWindow.

What is an iframe URL?

URL. Specifies the URL of the document to embed in the iframe. Possible values: An absolute URL - points to another web site (like src="http://www.example.com/default.htm") A relative URL - points to a file within a web site (like src="default.

What can I use instead of iframe?

Use the object Tag as an Alternative to Iframe in HTML We can use the tag to display another webpage in our webpage. The object tag is an alternative to the iframe tag in HTML. We can use the tag to embed different multimedia components like image, video, audio, etc.


2 Answers

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.

like image 79
kkyy Avatar answered Sep 28 '22 06:09

kkyy


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!

like image 43
ParPar Avatar answered Sep 28 '22 08:09

ParPar