The Little Guy Posted October 30, 2010 Share Posted October 30, 2010 Is there any way to watch for when the url changes? for example, say I am here: http://somesite.com/#key=value1 then say I click a link and it takes me here: http://somesite.com/#key=value2 is there anyway to detect the change? Link to comment https://forums.phpfreaks.com/topic/217259-on-url-change/ Share on other sites More sharing options...
trq Posted October 30, 2010 Share Posted October 30, 2010 There are ways of doing it, but it requires I little bit of work. I assume this is so you can have Ajax functionality while still being able to bookmark. The way I have done it in the paste is to use a simple MVC pattern. You need a controller function which is called on every click, this then checks the url and executes a function accordingly. Link to comment https://forums.phpfreaks.com/topic/217259-on-url-change/#findComment-1128241 Share on other sites More sharing options...
The Little Guy Posted October 30, 2010 Author Share Posted October 30, 2010 Yeah your pretty much right Right now, I have a function called loadBody(). this loads the body, and changes the url (like posted above). After I posted this, and did some more research, the only conclusion I could come to was to set an interval. The main reason I want this is so I can have a "history", because if I press the back button nothing would happen. But when I added the following code, it works, but I am not sure how hard that is on the browser, especially if it is run for say an hour. var interval = setInterval('watchWindow()', 200); function watchWindow(){ var wlocation = window.location.hash.toString(); if(wlocation != clocation){ var loca = window.location.toString(); var qstring = loca.match(/#(.+)/); if(qstring != null) var qstrings = qstring[1].split('&'); if(qstring != null){ for(var i=0; i<qstrings.length;i++){ tmp = qstrings[i].split('='); if(tmp[0]=='v') v = tmp[1]; else{ ext = tmp[0]; val = tmp[1]; } } } clocation = wlocation; loadBody(v, ext, val); } } Does that all make sense? Link to comment https://forums.phpfreaks.com/topic/217259-on-url-change/#findComment-1128243 Share on other sites More sharing options...
.josh Posted October 30, 2010 Share Posted October 30, 2010 What about keeping track of it with a session array? Link to comment https://forums.phpfreaks.com/topic/217259-on-url-change/#findComment-1128393 Share on other sites More sharing options...
The Little Guy Posted October 30, 2010 Author Share Posted October 30, 2010 How would that work? If I press the back button, nothing happens. How would a session help? Link to comment https://forums.phpfreaks.com/topic/217259-on-url-change/#findComment-1128400 Share on other sites More sharing options...
.josh Posted October 30, 2010 Share Posted October 30, 2010 oh I thought you were wanting to make your own "back" button, like a link of your own, not the actual browser back button. Link to comment https://forums.phpfreaks.com/topic/217259-on-url-change/#findComment-1128415 Share on other sites More sharing options...
The Little Guy Posted October 30, 2010 Author Share Posted October 30, 2010 What I have seems to work fine... I have left the site running for 8+ hours, and there seems to be no cache build up or anything slowing down my computer or browser. But... I have only tried that with Chrome, so that doesn't mean a different browser wouldn't start to slow down the computer and/or browser. Any thoughts? Link to comment https://forums.phpfreaks.com/topic/217259-on-url-change/#findComment-1128418 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.