Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial 
   
    alex mattingley
7,508 Pointsonhashchange js event handler is not triggering with a url change.
I am working to build a single page app using both php and js/ajax. In order to detect when a user pushes the back button and wants to return to a previous page, I am using the js method onhashchange. I cannot figure out for the life of me why it is not triggering when the url changes.
Documentation on onhashchange from MDN:
https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onhashchange
A relevant stack overflow question:
http://stackoverflow.com/questions/25806608/how-to-detect-browser-back-button-event-cross-browser
I am using the last solution here that he/she talks about in the stackoverflow answer. Here is my code:
document.onmouseover = function() {
//User's mouse is inside the page. (This is working)
    window.innerDocClick = true;
}
document.onmouseleave = function() {
//User's mouse has left the page. (this is working)
    window.innerDocClick = false;
}
window.onhashchange = function() {
    console.log("on hash"); //This is not triggering, and it should be on each url change I think.
    if (window.innerDocClick) {
        console.log("you never left the window");
    } else {
        //Browser back button was clicked
        console.log("back button!");
    }
};