I will give a solution that also involves some other problems that I have suffered with my client and that many people have said that it is impossible to get around.
Well, it follows the commented code, where it only allows the output message of the page to be displayed when the reload button is clicked, or when the client goes to a new page . For example typing a new address in the URL bar .
BUT, if he press F5 , or CTRL + R , or BackSpace , or / strong> from the browser it performs that action, but does not display the exit alert message .
In the case in our company, it meets:
// para o botao voltar do navegador
detectedBrowser= Qual_Eh_o_Seu_Browser?;
if(detectedBrowser==InternetExplorer){history.pushState( "nohb", null, "URL_Corretaaaaa" );}
else {history.pushState( "nohb", null, "" );}
window.onpopstate = function(event) { // JS version
// $(window).on( "popstate", function(event){ // Jquery version
console.log('MESSAGE 213 = ' + flag_beforeunload )
if( !event.state ){ // JS version
// if( !event.originalEvent.state ){ // Jquery version
// para desabilitar o botao BACK do navegador, descomente as linhas abaixo:
// history.pushState( "nohb", null, "" );
// return;
// para tratar o evento UnBeforeUnload voce tem as linhas abaixo.. modifique conforme sua necessidade... neste exemplo ele so volta uam pagina, sem pop up.
flag_beforeunload=false;
window.history.back();
}
};
//
//escutando o teclado
//
document.onkeydown = KeyCheck;
function KeyCheck(e) {
var key = (window.event) ? event.keyCode : e.keyCode;
if(key==116) {flag_beforeunload=false;}
if(key==8) {flag_beforeunload=false;}
if (e.keyCode == 82 && e.ctrlKey) {flag_beforeunload=false;}
if(e.keyCode == 91) {flag_beforeunload=false;} // safari
if (e.keyCode == 82) {flag_beforeunload=false;} // safari
document.getElementById("container").innerHTML = "key = "+key + " - " + flag_beforeunload;
}
//
// o evento UnBeforeUnload
//
window.onbeforeunload = function(e){
var msg = 'Deseja mesmo sair?';
e = e || window.event;
if(e)
if (flag_beforeunload == true) {
return msg;
}
}
Please use caution, remember that it is an example ....
so this is far from perfect !! should be appropriate to your needs
Thank you!
It works fine in chrome, Jah in firefox, iexplorer and safari I'm still seeing ways to work.
ps: I also put it in the fidle, but this page is not working completely .. please make a place for tests ...