问题
$window 'popstate'
event is not working in IE on browser back button. Below is the code snippet which is getting used to remove some modal classes on back button.
$(window).on('popstate', function(event) {
event.preventDefault();
event.stopPropagation();
$('.modal-backdrop').remove();
$('body').removeClass( 'modal-open' );
});
Issue - when Modal is open and on click of browser back button trying to remove modal classes on previous page which is coming after click of browser back button.
In chrome it is working fine but in IE it's not going inside 'popstate'
Any other way to remove classes on browser back button will be appreciated.
回答1:
You can try using hashchange
event instead on IE like:
function onHistoryChange(event) {
event.preventDefault();
event.stopPropagation();
console.log('On History Change');
$('.modal-backdrop').remove();
$('body').removeClass('modal-open');
}
if (window.document.documentMode) {
// This is IE, use hashchange instead
$(window).on('hashchange', onHistoryChange);
} else {
$(window).on('popstate', onHistoryChange);
}
来源:https://stackoverflow.com/questions/61569485/window-onpopstate-is-not-working-in-ie