简体   繁体   中英

How to capture back button click event in IE8?

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 ??

  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;
     };

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM