Daniel
Daniel

Reputation: 3711

Reload page on browser history back button

I want the page to reload on hitting the browser history back button. However, since the URL gets changed often using JavaScript's window.history.pushState I do not want to reload the page every time the location changes. On default the browser just changes the URL without reloading the page on hitting the back button.

(By this I would like to use the browser history back button as some kind of "undo" function.)

Upvotes: 1

Views: 2962

Answers (1)

Prashant
Prashant

Reputation: 190

Try this.

function HandleBackFunctionality(){
   //For IE
   if(window.event){
       //To check if its Back
       if(window.event.clientX < 40 && window.event.clientY < 0){
          alert("Browser back button is clicked…");
       //Its Refresh
       } else {
          alert("Browser refresh button is clicked…");
       }
   }
   //Other browsers
   else {
       if(event.currentTarget.performance.navigation.type == 1){
          alert("Browser refresh button is clicked…");
       }
       if(event.currentTarget.performance.navigation.type == 2){
          alert("Browser back button is clicked…");
       }
   }
}

You need to call this method on onbeforeunload event.

<body onbeforeunload="HandleBackFunctionality()">

This method, however, does not work with keyboard controls.

Upvotes: 0

Related Questions