Change hash without triggering a hashchange event

后端 未结 2 452
借酒劲吻你
借酒劲吻你 2021-02-04 23:30

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 has

2条回答
  •  忘了有多久
    2021-02-05 00:12

    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/

提交回复
热议问题