1

I want to call a function when the hash changes using pure JavaScript. I tried putting this function on document.ready but it didn't work.

function hashHandler(){
    this.oldHash = window.location.hash;
    this.Check;

    var that = this;
    var detect = function(){
        if(that.oldHash!=window.location.hash){
            alert("HASH CHANGED - new has" + window.location.hash);
            that.oldHash = window.location.hash;
        }
    };
    this.Check = setInterval(function(){ detect() }, 100);
}
ajp15243
  • 7,704
  • 1
  • 32
  • 38
user1161310
  • 3,069
  • 3
  • 21
  • 27
  • On which event are you triggering the function? – user3334871 Nov 25 '14 at 20:43
  • "Pure Javascript"? As in [Vanilla JS](http://vanilla-js.com/), or don't you even want to use the appropriate DOM function such as [`window.addEventListener('hashchange', …)`](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers.onhashchange)? – Kijewski Nov 25 '14 at 20:43
  • im writing an extension for a website that when visiting other pages of that website, it only reloads a certain div, not the entire website. i would use anything that doesn't have me download and install something extra, i'm already using jquery so thats fair game. nothing i've tried works so far, even the 'duplicate' question. – user1161310 Nov 25 '14 at 20:53

0 Answers0