PDA

View Full Version : How to detect iframe src after serverside redirect?



Birmingham
07-16-2008, 04:08 PM
Hi everyone, I'm looking for a solution to this problem...

re: http://art.bwdp.org.uk/

If you click the RANDOM button, the iframe will load a fixed url which will then be redirected on the serverside to a random url. Then when you click Save, it detects the iframe's src from before it was redirected and displays the old src in an alert.

Does anyone know... How can I detect the correct iframe src, using JavaScript?

jscheuer1
07-16-2008, 05:36 PM
You have set the src attribute (when doing a random image) of the iframe to:


v1.php

Now, at least as far as I can tell, there is something about that page that generates its own unique id for itself when it loads or something like that, but however it is done, it is done after you set the iframe's src attribute. So querying the iframe's src attribute will only give you:


v1.php

However, since the iframe's location has changed, this will give you what you are after:


window.frames[0].location.href

Example usage, replace:


onclick="alert(document.getElementById('DisplayFrame').src);"

with:


onclick="alert(window.frames[0].location.href);"

Notes: This will only work for pages on the same domain (not a problem with your demo pages, if the situation changes and you spread this out across more than one domain, there could be problems). The [0] refers to the first frame (in this case iframe) in the window. Iframe's locations must be queried of the iframe as a part of the window's frames collection, not as part of the document's elements collection.

An alternative would be to generate the random query string id on the top page instead, then set the src attribute of the iframe, with the page v1.php not generating a unique id for itself. Then you could query the iframe's src attribute and get an accurate report.

Birmingham
07-17-2008, 09:39 AM
Thanks, jscheuer1. It works. :)