Traté de detectar un evento de cierre de toque y recibí un mensaje de confirmación. El código es así. Extrañamente, si cierro la pestaña después de que ocurre el evento del mouse (haciendo clic en cualquier lugar), el mensaje de confirmación aparece normalmente, pero si el evento del mouse no ocurre, la ventana del mensaje no aparece y simplemente aparece. ¿Cuál es la diferencia entre que ocurra un evento del mouse y no ocurra después de cargar la página?
$(function(){ var validNavigation = false; //Attach the event keypress to exclude the F5 refresh (includes normal refresh) $(document).bind('keypress', function (e) { if (e.keyCode == 116) { validNavigation = true; } }); // Attach the event click for all links in the page $("a").bind("click", function () { validNavigation = true; }); // Attach the event submit for all forms in the page $("form").bind("submit", function () { validNavigation = true; }); // Attach the event click for all inputs in the page $("input[type=submit]").bind("click", function () { validNavigation = true; }); window.onbeforeunload = function () { if (!validNavigation) { return "Do you really want to close?"; } validNavigation = false; }; });