0

I want to get updated url of iframe using javascript?

Like this:

<iframe src="http://yahoo.com" id="ifrm" onload="iframeurl()" ></iframe> <script> function iframeurl() { var ifrm = document.getElementById("ifrm"); alert(ifrm.src); } </script> 

but the code above doesn't work. How can I do this? I'll use external links in iframe which is not hosted on same domain where I'm using the code above.

Please Help.

Thanks

thanks again. http://yahoo.com is just an example. Again my domain is szacpp.com and I need to get the links of other.com. You mean I need to put htaccess in other.com while I don't have access the domain.

2
  • Try this by adding iframurl() after function declaration. Or add it to on document load function. Commented Jul 1, 2012 at 10:06
  • szacpp - I have updated my answer, hopefully this is useful to you. Commented Jul 3, 2012 at 7:42

2 Answers 2

3

The src attribute of an iframe will not update when the page changes within the iframe.

Your only option would be to query the document in the iframe, however that will only work if the page in the iframe comes from the same domain (otherwise you hit the same origin policy)

UPDATE

IF you have access to other.com, then you should be able to set Access-Control-Allow-Origin in the .htaccess file (assuming you are using a server that uses that file system).

You will find more information...

If you do not have access to other.com then you are simply out of luck. It will be impossible for you to know what the new page is, because the src attribute of the iframe will not be updated when the page changes within the iframe. And you will not be able to use JavaScript due to the same origin policy that I (or more precisely, Quentin) mentioned earlier.

Sign up to request clarification or add additional context in comments.

11 Comments

Cross site scripting refers to a class of code injection vulnerability and is largely unrelated to cross origin communication. I've changed the reference.
hi, Quentin thanks for quick response. You understood my problem. I used .htaccess with the line: Header add Access-Control-Allow-Origin * but still not working.
also I'm receiving (Refused to set unsafe header "User-Agent")
You can't set request headers on the server (since the server sends responses, not requests). CORS is used by a server to give permission for itself to be accessed, you can't set response headers on yahoo.com.
thanks again. yahoo.com is just an example. Again my domain is szacpp.com and I need to get the links of other.com. You mean I need to put htaccess in other.com while I don't have access the domain.
|
-1

try this :

<iframe src="http://yahoo.com" id="ifrm" onload="iframeurl()" ></iframe> <script> function iframeurl() { var ifrm = parent.document.getElementById("ifrm"); alert(ifrm.src); } </script> 

2 Comments

It took me a while to spot-the-difference (please highlight changes when you copy/paste entire blocks of code). Assuming this is the top level frame, adding parent. will make no difference. If it isn't the top level frame, it will break the attempt to get the iframe element by its id. This won't help at all and might make things worse.
This could never work, as the iframe object lives in the same document as the script, therefore by using parent you're referring to the parent of the document, not the parent of the iframe

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.