Arat Kumar rana
Arat Kumar rana

Reputation: 187

how to detect any browser back button click using java script or jquery?

Could you please let me know how to detect any browser back button click using java script or jquery?

I am using the below :

if(window.history && window.history.pushState){
            window.history.pushState({page:this.href}, null, 'this.href');
            $(window).on('popstate', function() {
              event.preventDefault();
              $('#releaseLicenseApp').modal();
            });
        }

but it is not working in chrome and ie and firefox.

Upvotes: 1

Views: 3651

Answers (2)

JoelP
JoelP

Reputation: 439

As you mentioned in the comments, you only want to notify the user that there are unsaved changes when he tries to leave the page. This can be done using onbeforeunload:

window.onbeforeunload = function(){
  return 'Please save your changes before navigating to other page, if you continue then your unsaved data will be lost.';
};

Upvotes: 2

sujeet gupta
sujeet gupta

Reputation: 149

Please try this on , it's working on chrome, ie and firfox

<script type="text/javascript">
            window.history.forward();
            function noBack()
            {
                window.history.forward();
            }
    </script>
    <body onLoad="noBack();" onpageshow="if (event.persisted) noBack();" onUnload=""> 

Upvotes: 0

Related Questions