Surya sasidhar
Surya sasidhar

Reputation: 30313

How to get url of the website in iframe?

how can i get url of the website which is in iframe, when i click on any links in website in iframe it is redirect to another page in the iframe then how can i get the page url. can u help me. thank you.

Upvotes: 0

Views: 1555

Answers (3)

Mouhannad
Mouhannad

Reputation: 2209

You can use jquery to make it easier:

alert($("#iframeid").attr("src"));

You can also use jquery contents() to retrieve or manipulate any tags inside that iframe. example:

$("#iframeid").contents().find("a").css("background-color","red").end().find("title").text();

Upvotes: 1

Adam
Adam

Reputation: 28858

From javascript? If you can run some JS inside the iframe:

alert(document.location.href);

If you can't - need to get a reference to the iframe in question:

IE:

alert(document.getElementById(iframeId).contentWindow.document.location.href);

FF, Safari, Chrome, etc:

alert(document.getElementById(iframeId).contentDocument.location.href);

As mentioned - you wont be able to do this if the URL that is loaded is not from the same domain as your website.

Upvotes: 0

ashicus
ashicus

Reputation: 1252

Unfortunately, you don't really have much control over an Iframe once it loads. I think pretty much the only thing you have control over is the ability to reload it with a new URL programatically.

If the page loaded by the Iframe is part of your website (not 3rd party), you can process the request server-side.

Upvotes: 0

Related Questions