PDA

View Full Version : <Frames> Detect When URL Changes



funkyspyspy
09-25-2010, 10:46 AM
Is There A Js Code To Check If The Url Changes??

vwphillips
09-25-2010, 12:01 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<iframe id="tst" src="http://www.dynamicdrive.com/forums/newreply.php?do=newreply&noquote=1&p=237923" onload="alert('');"></iframe>

<input type="button" name="" value="Test" onclick="document.getElementById('tst').src='http://www.dynamicdrive.com/forums/forumdisplay.php?f=23';"/>
</body>

</html>

jscheuer1
09-25-2010, 12:09 PM
No. And the URL doesn't change, well it could if the resource it points to is moved. But that's a semantic/technical distinction. I think you want to know when the frame switches to another page. That does happen and cannot be detected exactly. There's no URL property or attribute of frame. There is a location property and a src attribute. But if you change the location property, which can only be done via its href property (in javascript or by clicking on a link or by submitting a form), and will change the page, the src attribute remains the same. If you change the src attribute (can only be done in javascript), which will also change the page, the location.href changes, but you cannot access it if it is to a page on another domain. If you know with certainty (probably impossible) that this will not be the case, you can poll the location.href, but then you aren't detecting the actual change. But it would be close enough for most cases.

The only cross browser aspect of this that can be detected reliably is the frame's onload event firing. But that also fires when the page in the frame is refreshed.

If the page in the frame starts out on your domain, using the location.href you can detect when and if it leaves your domain, and when it returns. And while it is on your domain you can evaluate its location.href to see if that has changed. When it is off of your domain, you can detect when its location changes or is refreshed, but you cannot tell which.

So it really depends upon what you need this for and whether or not the limitations imposed by the browser aren't severe enough to prevent that. I say "limitations imposed by the browser" because these are security restrictions built into all modern browsers, not some actual limitation to javascript.

molendijk
09-25-2010, 02:56 PM
You could do something like the following for files belonging to your own domain (but I don't know if this is what you want):

<head>
<script type="text/javascript">
function get_iframe_url(){

new_iframe_url=
window.frames.the_iframe.window.location.href.substring(window.frames.the_iframe.window.location.href.lastIndexOf('/')+1,window.frames.the_iframe.window.location.href.length)

document.getElementById('url').innerHTML='New iframe_url is '+new_iframe_url
}
</script>
</head>

<body>

<iframe src="test1.html" name="the_iframe"></iframe>

<div id="url"></div><br><br>

<a onmousedown="window.frames.the_iframe.window.location.replace('test1.html')" onmouseup="get_iframe_url()">load test1.html in iframe</a><br>

<a onmousedown="window.frames.the_iframe.window.location.replace('test2.html')" onmouseup="get_iframe_url()">load test2.html in iframe</a>

</body>
===
Arie Molendijk.

jscheuer1
09-25-2010, 05:43 PM
Sort of the right idea Arie. But what if the href is to a folder (to launch the index page in that folder)? And can this be tailored to work on any link that might target the frame?

It might get a bit messy because of the location.replace method - no history, though that might be an advantage.

This was for frame not iframe. It could probably be transferred to frame, but that would add a layer of complexity.

I didn't mean to appear negative. Rather I was trying to elicit from funkyspyspy what exactly the objective is here. Perhaps then we could tailor something to that objective.

So, funkyspyspy, just what are you trying to do?

If it's just a general question though, my original answer stands - No.

molendijk
09-26-2010, 12:55 AM
So, funkyspyspy, just what are you trying to do?
Yes, that's what we have to know first.
===
Arie.