$(window).on('popstate') is not working in IE

女生的网名这么多〃 提交于 2021-02-11 17:50:43

问题


$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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!