window hashchange event on internet explorer is not triggered

448 Views Asked by At

i have a simple function on javascript where if the window position based on the url hashtag, so after the document is ready i execute my function normally, also i have add a hashchange event where i call the same function.

Everything works as expected except on internet explorer, if the user presses the enter on address bar without changing the hash (if he changes it works) then the function is not executed, any idea how to catch the event that the user pressed enter on the same url with same hashtag ?

1

There are 1 best solutions below

2
Pete On

When the user presses enter in the URL bar it loads the page, even if the adress wasn't changed. So, in your case it would simply refresh the page.

To capture this event you can use the onbeforeunload javascript event. This event gives you the ability to determine the navigation type. Use event.currentTarget.performance.navigation.type to determine the type of navigation. This is working in IE, FF and Chrome.

function CallbackFunction(event) {
    if (event.currentTarget.performance.navigation.type == 1) {
        console.log("refreshing page");
    }
}

document.onbeforeunload = CallbackFunction;

Hopefully this was of some help to you!!