1

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.)

Daniel
  • 3,383
  • 4
  • 30
  • 61
  • Maybe HTML5 can help you, look that [http://stackoverflow.com/questions/17507091/replacestate-vs-pushstate][1] [1]: http://stackoverflow.com/questions/17507091/replacestate-vs-pushstate – Alien Jul 04 '14 at 07:18
  • Thanks for your comment. However, using `replaceState()` instead of `pushState()` kills the "undo" function. – Daniel Jul 04 '14 at 07:26

1 Answers1

0

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.

zengr
  • 38,346
  • 37
  • 130
  • 192
Prashant
  • 190
  • 1
  • 9
  • Gives me a "ReferenceError: event is not defined" in the console in Firefox. In Chrome there is no error, but the back button still does no refresh. – Daniel Jul 04 '14 at 07:40