How to detect a window hash change?

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

 if(window.location.hash.hasChanged()) { // ajax stuff } else { // nothing, no hash has been changed (without any window reload) } 

If I change the hash, for example. edits and changes the hash, pressing enter after editing, nothing happens, only when the window is reloaded it will detect a hash change.

+4
source share
2 answers

Ben Alman hashchange jQuery plugin provides the ability to track the hash for changes.

+2
source

The latest browsers (FF3.6 +, IE8, Chrome) support the "hashchange" event in the window object - see MDC: window.onhashchange for use. A simple asynchronous listening might look like this:

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

If you need an implementation that supports older browsers, things will get complicated pretty quickly, and I recommend using a different library or plugin - see the answers to this question for a few suggestions.

+12
source

Source: https://habr.com/ru/post/1336414/


All Articles