Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
124
rated 0 times [  131] [ 7]  / answers: 1 / hits: 21552  / 14 Years ago, fri, november 5, 2010, 12:00:00

I'm using the hash to load content dynamically. To make the back button work I am capturing hash changes. However sometimes I need to change the hash without triggering the hash changed function (eg, when the page was redirected server side and I need to update the hash once the content has returned.)



The best solution I have come up with is to unbind the hashchange event, make the change and then rebind it. However, as this happens asynchronously, I am finding that it rebinds too quickly and still catches the hash change.



My solution at the moment is very poor: Rebinding in a setTimeout. Does anyone have a better idea?



    $(window).unbind( 'hashchange', hashChanged);
window.location.hash = ! + url;
setTimeout(function(){
$(window).bind( 'hashchange', hashChanged);
}, 100);





Edit:

Amir Raminfar's suggestion prompted me to a solution that does not require a timeout.
I added a class variable



_ignoreHashChange = false;


When I want to change the hash silently I do this:



_ignoreHashChange = true;
window.location.hash = ! + url;


and the hash changed event does this :



function hashChanged(event){
if(_ignoreHashChange === false){
url = window.location.hash.slice(2);
fetchContent(url);
}
_ignoreHashChange = false;
}

More From » jquery

 Answers
18

You can have a function like this:



function updateHash(newHash){
...
oldHash = newHash
}


then in your setTimeOut you need to do



function(){
if(oldHash != currenHash){
updateHash(currenHash);
}
}


So now you can call update hash manually and it won't be triggered by the event. You can also have more parameters in updateHash to do other things.



By the way, have you looked at the jquery history plugin? http://tkyk.github.com/jquery-history-plugin/


[#95066] Wednesday, November 3, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elians

Total Points: 417
Total Questions: 87
Total Answers: 101

Location: Barbados
Member since Sun, Nov 27, 2022
2 Years ago
;