1

I am trying to execute a function when the user clicks on the back button of a browser. For chrome & firefox, I have achieved it with html5 "popstate" event. I'm not being able to capture the back button click event in IE8. In my case, I need to capture the back button click event even if the url/location hash doesn't change. Anyone to help ??

Silent_Rebel
  • 300
  • 4
  • 15

2 Answers2

0
  window.onbeforeunload = function (e) {
        var e = e || window.event;
        var msg = "Do you really want to leave this page?"
        // For IE and Firefox
        if (e) {
            e.returnValue = msg;
        }
        // For Safari / chrome
       return msg;
     };
Shijin TR
  • 7,516
  • 10
  • 55
  • 122
  • Thanks for your time.. I guess,, this will work for refresh button also.. My requirement is to catch only back button click event.. – Silent_Rebel Mar 21 '14 at 09:05
0

Well, from google search I found this link, it seems that you can differentiate between a refresh and a click back:

window.onunload = function(e) {
// Firefox || IE
e = e || window.event;

var y = e.pageY || e.clientY;

if(y < 0)  alert("Window closed");
else alert("Window refreshed");

}

event.pageY | event.clientY: detect the location the mouse.
window.onunload:is responsible for executing an instruction when the page is closed.

I didn't try it myself, but hope this would help.

Community
  • 1
  • 1
user2517028
  • 784
  • 1
  • 11
  • 25