Jquery when back button is pressed

You can sort of do it using the popstate event:

window.onpopstate = function() {
  alert("pop!");
}

or in jQuery:

$(window).on('popstate', function(event) {
 alert("pop");
});

However this will also trigger when navigating forward, not only backward.


Disable back url

$(function() {
    if (window.history && window.history.pushState) {
        window.history.pushState('', null, './');
        $(window).on('popstate', function() {
            // alert('Back button was pressed.');
            document.location.href = '#';

        });
    }
});