简体   繁体   中英

window popstate event is not triggering in firefox and chrome

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.

You can bind an event listener with javascript like this:

window.addEventListener('popstate', function(event) {
    alert( "triggered" );
});

I believe that you need add first an history before detect back? Example:

var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "?foo=1");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM