Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery window.on('hashchange') event not firing
    text
    copied!<p>I am using the jquery.on method on the window object to bind the hashchange event. But the first time this happens. Nothing seems to happen</p> <p>this is my code:</p> <pre><code>PageLoad = { ignoreHashChange: false, hashChanged: function(oldURL) { if (this.ignoreHashChange) { this.ignoreHashChange = false; return; } /* snip */ }, init: function() { $(window).on('hashchange', function(e) { this.hashChanged(e.originalEvent.oldURL); }.bind(this)); }, updateHash: function(hash) { if ($._data(window, "events").hashchange != undefined) { this.ignoreHashChange = true; } window.location.hash = hash; } }; </code></pre> <p>In the document ready some ajax call is executed, this returns me some data and I insert that into the body. I update the hash by using the updateHash method. I specifically check if the hashchange event is bound on the window object before I set the ignoreHashChange property to false. (setting it to false makes sure my application doesn't go back into a 'route' action which would re-load the same page.</p> <p>However, when updateHash is called (for the first time) it detects that the window has indeed the hashchange event bound to it, so it sets the ignoreHashChange to true. After I adjust the window.location.hash, but the hashchange event on the window is not triggered. </p> <p>If I alter the hash manually later, this.ignoreHashChange is still set to true, so it ignores the change. </p> <p>Everything after that works as expected. Calling PageLoad.updateHash('hash'); changes the hash, but doesn't route the application. Changing the hash manually changes the hash, and routes the website to the new content.</p> <p>The only thing I can think of why this is happening. Jquery must still be setting up the window.onhashchange event when the first window.location.hash is called so that the hashchange event that is bound (in jquery) isn't fired yet since it isn't actually bound to the document yet.</p> <p>Anyone has any ideas or similar issues in the past that could cause this behaviour?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload