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 circumvent.
Well, it follows the commented code, where it only allows the outgoing message the page is displayed when the Reload button is clicked, or when the client goes to a new page. For example by entering a new address in the URL bar..
BUT IF HE SQUEEZES F5, or CTRL+R, or Backspace, or give click on the Back button of the browser it performs that action, but does not display the page output 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 carefully, remember that is an example....
then this is far from perfect!! must be suitable to your needs
thank you!
It works well on Chrome, Jah in firefox, iexplorer and safari I’m still seeing ways to work.
ps: also put in the fidle, but on this page not working through.. please knife site for testing...
window.history.back();
?– Caio Felipe Pereira
You can explain the functionality you want to have using
onbeforeunload
? it will be difficult to separate these events.– Sergio
@Sergio made an example of Script, actually I want to undo the back button of onbeforeunload
– MoisesGama