Reputation: 7375
I want to detect the backbutton click event so am using window popstate event in jquery. but it is not at all firing in any time.
please refer below code
$(document).ready(function(){
$(window).on("popstate",function(){
debugger
});
});
tried this way too.
window.onpopstate = function(event) {debugger
alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};
is there any external script need to add for that ? or anything else need to confiure in my page.
Upvotes: 1
Views: 5146
Reputation: 1188
I believe that you need add first an history before detect back? Example:
var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "?foo=1");
Upvotes: 1
Reputation: 1715
You can bind an event listener with javascript like this:
window.addEventListener('popstate', function(event) {
alert( "triggered" );
});
Upvotes: 1