简体   繁体   中英

Solution for, “onpopstate” event which is not working in chrome, but working fine in IE and FireFox

When a user press the browser back button I want to alert the user, for that to capture the browser back button event I am using onpopstate event as show below. But the problem is the event is working fine in IE and FireFox but it is not being triggered in chrome. Kindly help me with this, Thanks.

$(window).on('popstate', function () {
    this.alert("Do you really want to leave the page?");
});

Using Chrome 75

The pop state event is only triggered if you have added one or more items to the history state. You can get around it without actually pushing by replacing the state on page load, with a default value, that should trigger your event.

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