0

How do I detect Back/Next/Refresh BUTTON click on Chrome?

I already have a solution for keyboard detection but it doesn't works for the buttons in the browser.

<script language="javascript" >
var validNavigation = false;
var numPendencias = 0;

function verificaPendencias() {

    var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
    var leave_message = 'Existem pendências ativas, deseja mesmo sair?'
    function goodbye(e) {
        if (!validNavigation) {
            if (dont_confirm_leave!==1) {
                if(!e) e = window.event;
                //e.cancelBubble is supported by IE - this will kill the bubbling process.
                e.cancelBubble = true;
                e.returnValue = leave_message;
                //e.stopPropagation works in Firefox.
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }                    
                //return works for Chrome and Safari
                return leave_message;
            }
        }
    }

    window.onbeforeunload=goodbye;

}

function atribuirEventos() {    
    jQuery(document).bind('keypress', function(e) {
        if (e.keyCode == 116){
          validNavigation = true;
        }
    });

    jQuery(document).bind('keydown', function(e) {
        if (e.keyCode == 116){
          validNavigation = true;
        }
    });

    jQuery(document).bind('keyup', function(e) {
        if (e.keyCode == 116){
          validNavigation = true;
        }
    });

    jQuery("a").bind("mouseover", function() {
        validNavigation = true;
    });                
    jQuery("a").bind("mouseout", function() {
        validNavigation = false;
    });

    jQuery("form").bind("submit", function() {
        validNavigation = true;
    });

    jQuery("input[type=submit]").bind("click", function() {
        validNavigation = true;
    });


    jQuery("button").bind("mouseover", function() {
        validNavigation = true;            

    });                
    jQuery("button").bind("mouseout", function() {
        validNavigation = false;

    });        
}


jQuery(document).ready(function() {
    verificaPendencias();
    atribuirEventos();
});    

Does It have some properties that shows this events?

To be more accurate, I would like to generate a warning when the user closes the browser or tab, but the problem is that through the onbeforeunload, every time he clicked on a link, pressed F5 or do anything that comes out the page itself it displayed the message, then I put the locks up, but even so when the user clicks on the buttons of the browser it generates the message, I wanted to avoid using the method above it and leave message only when the flap was closed or browser.

  • You can only work with the [`popstate`](https://developer.mozilla.org/en-US/docs/Web/API/Window.onpopstate) event, you can't detect the buttons separately. – SeinopSys Aug 26 '14 at 16:32
  • I tried to use it, but in my browser the back and next button doesn't work anymore. – brunodotcom Aug 27 '14 at 19:22

1 Answers1

0

I think you need to use something like:

window.onpopstate = function(event) {
    // do something  
};
Simcha
  • 3,300
  • 7
  • 29
  • 42