View Full Version : JS Eventlistener for URL change
inf
September 13th, 2009, 12:23 PM
hi everyone,
I tried to create an eventlistener which listens the browser url text and if it changes calls a function.
I tried in this way but it didn't work.
var link = document.location.href;
link.addEventListener('change',changeEvent,false);
function changeEvent(e) {
alert('url changed');
}
can anyone help me on this?
Swooter
September 14th, 2009, 10:55 AM
You can only use the window.onload, window.onunload and window.onbeforeunload methods. Reading the new URL is impossible due to security.
a tadster
September 14th, 2009, 12:35 PM
yep, what Swooter said ^
also there is the new onhashchange event that IE 8 and FF 3.6 support, if that suits your needs.
inf
September 14th, 2009, 12:59 PM
i made a flash test site to explain my problem better.
http://uploads.paralelart.com/inf/modifyurl/
it is a simple site, 3 buttons and 3 frames. if you click the third page button you can see that the browser's url changes (#p3 anchor point added at url end). if you copy and paste the url in a new browser window you can see flash goes to the third page. it works fine except one thing. if you rewrite the url in the browser (for example#p3 to #p2) and then press enter nothing happen.
i need an event if the anchor point changed calls a js function afterthat i can handle the rest code in flash.
any idea how can i solve this?
a tadster
September 14th, 2009, 02:03 PM
use a recursive timeout and a global variable:
var currentHash = "init";
function checkHash() {
if (window.location.href.match("#")) {
var theHash = window.location.href.split("#");
if (currentHash != theHash[theHash.length-1])
{tellFlash(theHash[theHash.length-1]);currentHash = theHash[theHash.length-1];}
if (currentHash == theHash[theHash.length-1]) {setTimeout("checkHash()", "200");}
}
else {setTimeout("checkHash()", "200");}
}
function tellFlash(newHashValue) {
document.getElementById('myFlashesId').callBackFun ctionName(newHashValue);
}
however ie 7 does not treat the hash changing as expected.
have you tried SwfAddress?
inf
September 15th, 2009, 04:17 AM
thanks for the advice. swfaddress is what i needed. :hugegrin:
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.