javascript - How to destroy 'Popstate' event listener? - Stack Overflow

Have tried Below Code but its not destroying Popstate Event.Please help we with the example in which i

Have tried Below Code but its not destroying Popstate Event.

Please help we with the example in which i can destroy the Popstate Event based on the condition.

history.pushState(null, document.title, location.href);
window.addEventListener('popstate', function (event) {
  if (true){
    history.pushState(null, document.title, location.href);
    console.log('Back Button Prevented');
  } else {
      window.removeEventListener('popstate', ()=> {
          console.log('Go back');
      }, true);
      history.back();
  }
});

Have tried Below Code but its not destroying Popstate Event.

Please help we with the example in which i can destroy the Popstate Event based on the condition.

history.pushState(null, document.title, location.href);
window.addEventListener('popstate', function (event) {
  if (true){
    history.pushState(null, document.title, location.href);
    console.log('Back Button Prevented');
  } else {
      window.removeEventListener('popstate', ()=> {
          console.log('Go back');
      }, true);
      history.back();
  }
});
Share Improve this question asked Apr 8, 2019 at 12:39 Hardik ShimpiHardik Shimpi 4101 gold badge7 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

In order to remove a listener, you have to pass the listener function itself to removeEventListener().

Another problem in your code is that with the use of if (true), you'll never reach the else block that is removing the listener. What you'll probably want to do is have a boolean variable outside of the listener that you change on the first call of the listener.

var backButtonPrevented = false;
history.pushState(null, document.title, location.href);

function popStateListener(event) {
  if (backButtonPrevented === false){
    history.pushState(null, document.title, location.href);
    console.log('Back Button Prevented');
    backButtonPrevented = true;
  } else {
      window.removeEventListener('popstate', popStateListener);
      history.back();
  }
}

window.addEventListener('popstate', popStateListener);

The second argument you pass to removeEventListener has to be the function you want to remove.

You are passing a different function, which hasn't been registered as an event handler, so nothing happens.

Declare your event handler as a function with a reference you can reuse. Then use that reference for both removeEventListener and addEventListener.

history.pushState(null, document.title, location.href);

function myEventListener (event) { 
  if (true){
    history.pushState(null, document.title, location.href);
    console.log('Back Button Prevented');
  } else {

      window.removeEventListener('popstate', myEventListener);
      history.back();
  }
}

window.addEventListener('popstate', myEventListener);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742252237a4409369.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信