Trying to adopt a simple method to fix back button when using AJAX

B

bizt

Hi,

After some sniffing about ive been trying to find a simple means to
fixing the back button for my AJAX site. One means I did come across
was using the url hash to store state information. Because I could
find no event that triggered that the back button was clicked (only
know of onunload but this seems to fire for any type of relocation) so
I have a function that repetitively checks (every half second) the
current hash against a stored hash. If the hash changes, the condition
runs that updates the state and stores the current hash value as the
stored hash. When state is updated on screen, it will update the hash
value thus updating the page history

This I think will do what I require and Im glad that ive managed to
put together a simple fix instead of using an existing js library that
does loads of other things that i dont need and i dont really know its
full working internally. Im a bit concerned that having a function
being called a few times every second isnt ideal but since no back
button event seems to exist i dont know if i have a choice here.

Anyway, would be great it someone could tell me what faults if any lie
with this technique or maybe have a much better idea. Code below:

var sHash = '';
var oHashCheckTimeout;

function InitialiseHashCheck() {
sHash = window.location.hash;
HashCheck();
}
function HashCheck() {
clearTimeout(oHashCheckTimeout);
if(sHash != window.location.hash) {
// restore state code here...

// set timeout
sHash = window.location.hash;
}
oHashCheckTimeout = setTimeout("HashCheck()",500);
}

window.onload = function() {
InitialiseHashCheck();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top