0

I tried the following code:

window.onbeforeunload = function() { return "Your work will be lost."; };

But even I click on a button of my page, I get an alert message. I want to detect just the back button event. How can I do that?

senior
  • 2,196
  • 6
  • 36
  • 54
  • 1
    To give further context: what are you trying to ultimately achieve? *"even when I click on a button"*... You mean a button that would cause the user to leave the page, correct? – Wesley Murch May 20 '14 at 17:05
  • I literally just typed your question on google and found the answer here on stackoverflow – William Barbosa May 20 '14 at 17:05
  • [There you go](http://stackoverflow.com/questions/17594413/js-or-jquery-browser-back-button-click-detector) – William Barbosa May 20 '14 at 17:08

2 Answers2

0

window.onbeforunload = ... will run the function when ever the page is left. since the back button is liked to the browser and not the site, its hard to edit its function, but this link helps a lot when trying to get around this http://www.irt.org/script/311.htm. hope this helps :)

Sam Denton
  • 465
  • 3
  • 18
0

The best solution I found is to add the following code in the script block:

<script type="text/javascript">
window.location.hash="no-back-button"; 
window.onhashchange=function(){
   window.location.hash="no-back-button";
}
// your code

</script>
senior
  • 2,196
  • 6
  • 36
  • 54