Results 1 to 6 of 6

Thread: <Frames> Detect When URL Changes

  1. #1
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default <Frames> Detect When URL Changes

    Is There A Js Code To Check If The Url Changes??

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!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>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,854
    Thanks
    49
    Thanked 259 Times in 251 Posts
    Blog Entries
    56

    Default

    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):
    Code:
    <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.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,854
    Thanks
    49
    Thanked 259 Times in 251 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by jscheuer1 View Post
    So, funkyspyspy, just what are you trying to do?
    Yes, that's what we have to know first.
    ===
    Arie.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •