views:

248

answers:

4

Hi, I am using JavaScript to make a small iframe application, and I cannot seem to figure out a way to update the URL in my URL bar I made when someone clicks a link inside the iframe.

It needs to be instantaneous, and preferably without checking every millisecond whether or not the value of document.getElementById('idofiframe').src has changed.

I can't seem to find a simple property to tell when the url has changed, so if there is not one, then solving this programmatically will work as well.

Thanks for the help!

A: 

Instead, can you add code to the frame's contents to have it raise an event to the container frame?

In IE, the "OnReadyStateChanged" event might give you what you want.

David
The iframe content will most likely not be my own. At all. In fact, its starting page is google. Thanks for the suggestion though!
Cyclone
+1  A: 

This will be difficult to do because it is considered xss and most browsers block that.

There are most likely some workarounds involving AJAX.

Jcubed
+1  A: 

Hey Cyclone.

First of all, what you want to do will be possible only if the source of your iframe points to the same domain as the parent window. So if you have a page page.html that iframes another page iframed.html, then both of them have to reside on the same domain (e.g. www.example.com/page.html and www.example.com/iframed.html)

If that is the case, you can do the following in the iframed.html page:

<script type="text/javascript">
window.onload = function() {
    var links = document.getElementsByTagName('a');
    for (var i=0, link; link = links[i]; i++) {
     link.onclick = function() {
      window.parent.location.href = '#' + encodeURIComponent(this.href);
     }
    }
}
</script>

This will make it so that whenever you click on a link in iframed.html, the url bar will put the url of the link in the "hash tag" of the url (e.g. www.example.com/page.html#http%3A%2F%2Fwww.example.com%2FanotherPage.html)

Obviously, you would have to have a script like this on every page that is to appear inside the iframe.

Once this is in place, then you can put this snippet inside of page.html, and it will make the iframe automatically load the url in the hash tag:

window.onload = function() {
    var url = window.location.hash.substr(1);
    if (url) {
     document.getElementById('iframe').src = url;
    }
}

I unfortunately haven't run this code to test it, but it is pretty straight forward and should explain the idea. Let me know how it goes!

Marcus Westin
This is a very nice solution, but I unfortunately am not the owner of the websites inside the iframe, so I cannot place this code. If I tried to find out the window location and it was not my site, that would then be considered XSS and would not be a solution.Thanks for the help though! Ill let you know how this goes.
Cyclone
If that's the case I'm afraid you're out of luck - you will be able to look at the src attribute of the iframe, but you will never have access to the contentWindow of the iframe. When the iframe window navigates, its contentWindow.location updates, but the src of the iframe element stays the same.There is one way, but it requires the iframed page to cooporate - it can itself open an iframe to a page on your domain, and pass in its own url. A similar technique is used by http://oframebust.com/
Marcus Westin
+1  A: 

You could add an onload event to the iframe and then monitor that - it'll get thrown whenever the frame finishes loading (though, of course, it could be the same URL again...)

Mat Mannion
Okay, I think I will try this first. Thanks! If it is the same URL, its not a problem at all.
Cyclone