How to detect change of window hash?

前端 未结 2 1018
野性不改
野性不改 2021-01-19 21:43

How can you detect a window.location.hash onchange, for example I could do this:

if(window.location.hash.hasChanged())
{
   // ajax stuff
}
else         


        
相关标签:
2条回答
  • 2021-01-19 22:33

    Ben Alman's hashchange jQuery plugin provides the ability to monitor the hash for changes.

    0 讨论(0)
  • 2021-01-19 22:43

    Most recent browsers (FF3.6+, IE8, Chrome) support the "hashchange" event on the window object - see MDC: window.onhashchange for usage examples. Simple asynchronous listening might look like:

    window.onhashchange = function() {
       // do something awesome here
    };
    

    If you want an implementation that supports older browsers, things get complicated quite quickly, and I recommend using someone else's library or plugin - see answers to this question for several suggestions.

    0 讨论(0)
提交回复
热议问题