Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to e.preventDefault in window.onPopState?

I'm trying to stop the user from going back in my web app. For this I tried catching the window.onpopstate and added e.preventDefault to cancel the back button effect.

But it doesn't seems to happen.

window.addEventListener('popstate',function(e){ 
console.log(e); e.preventDefault();  
});

Is it not possible to prevent the popstate event of browser? Or am I doing something wrong?

like image 808
Ivin Avatar asked Sep 07 '15 06:09

Ivin


People also ask

What is window Onpopstate?

The window. onpopstate event is fired automatically by the browser when a user navigates between history states that a developer has set. This event is important to handle when you push to history object and then later retrieve information whenever the user presses the back/forward button of the browser.

Does history back trigger Popstate?

The popstate event will be triggered by doing a browser action such as a click on the back or forward button (or calling history. back() or history. forward() in JavaScript). Browsers tend to handle the popstate event differently on page load.


1 Answers

According to this documentation, the popstate event is not cancellable:

Specification: HTML5
Interface: PopStateEvent
Bubbles: Yes
Cancelable: No
Target: defaultView
Default Action: None

like image 149
Patrick Klug Avatar answered Sep 18 '22 11:09

Patrick Klug