onbeforeunload

window.onbeforeunload - Only show warning when not submitting form [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-11-28 08:04:06
This question already has an answer here: How to disable beforeunload action when user is submitting a form? 6 answers I am using window.onbeforeunload to prevent the user from navigating away after changing values on a form. This is working fine, except it also shows the warning when the user submits the form (not desired). How can I do this without showing the warning when the form submits? Current code: var formHasChanged = false; $(document).on('change', 'form.confirm-navigation-form input, form.confirm-navigation-form select, form.confirm-navigation-form textarea', function (e) {

window.onbeforeunload executed on page refresh instead of on page close

*爱你&永不变心* 提交于 2019-11-28 03:06:04
问题 I'm using window.onbeforeunload to pop up a confirm dialog when a close event occurs, but the confirm dialog appears on page refresh and doesn't execute on page close. Here's the JavaScript code: <script language="JavaScript"> window.onbeforeunload = confirmWinClose(); function confirmWinClose() { var confirmClose = confirm('Close?'); return confirmClose; } </script> I tried it on Chrome, Firefox and Internet Explorer. 回答1: PROBLEM WITH YOUR CODE: the function will be called when you refresh

PHP update database on close window

余生颓废 提交于 2019-11-28 02:23:55
问题 I am trying to update the database when the user close or refresh the browser. This is my code: $(window).bind('unload', function(){ $.ajax({ type: 'get', async: false, url: 'liberaLugar.php?idCurso=2' }); }); Any help will be appreciated. Thanks in advance! 回答1: This should work for you in most cases. of course, if your server is unreachable, you could freeze the browser, so we should also add a timeout. it may also be helpul to give your user some visual indicator of what the hell is going

how to identify onbeforeunload was caused by clicking close button

穿精又带淫゛_ 提交于 2019-11-28 02:03:33
How do I determine if onbeforeunload was caused by clicking the close button or a page refresh or generally what event caused onbeforeunload? here is a snippet: window.onbeforeunload = function( e ){ if( !e ) e = window.event; // insert code to determine which is the source of the event } Please help me. Referring to various articles and doing some trial and errors, finally developed this idea which works perfectly for me just the way i wanted it to happen. The logic was quiet simpler it implement as well The idea was to detect the unload event that is triggered by closing the browser. In that

onbeforeunload in Opera

我的梦境 提交于 2019-11-28 00:50:40
I'm using the code that netadictos posted to the question here . All I want to do is to display a warning when a user is navigating away from or closing a window/tab. The code that netadictos posted seems to work fine in IE7, FF 3.0.5, Safari 3.2.1, and Chrome but it doesn't work in Opera v9.63. Does anyone know of way of doing the same thing in Opera? Thx, Trev onbeforeunload is now supported in Opera 15 based on the WebKit engine but not in any prior versions based on Presto. Opera does not support window.onbeforeunload at the moment. It will be supported in some future version, but has not

Javascript detect closing popup loaded with another domain

≯℡__Kan透↙ 提交于 2019-11-27 18:23:32
I am opening a popup window and attaching an onbeforeunload event to it like this: win = window.open("http://www.google.com", "", "width=300px,height=300px"); win.onbeforeunload = function() { //do your stuff here alert("Closed"); }; If I leave the URL empty, the new popup opens with "about:blank" as the address but when I close it, I see the alert. If I open in as you see it (with an external URL), once it's closed, I cannot see the alert anymore. Any idea why this is happening? As mentioned, same origin policy prevents Javascript from detecting such events. But there's a quite simple

onBeforeUnload not working correctly for Chrome

梦想的初衷 提交于 2019-11-27 16:13:54
问题 I've been using this code for some time and it worked fine until yesterday: window.onbeforeunload = function() { return "¡Atención! Si continúas no podrás volver atrás y podrías perder datos. Revisa todos los campos y termina el trabajo antes de moverte de esta página. ¿Seguro que quieres continuar?"; } The problem now is that Chrome shows this: But Edge shows it the right way: What am I doing wrong? Note: Language is not the problem because I've tried with both English and Spanish. 回答1: You

Is the onbeforeunload event not supported on iPhone?

早过忘川 提交于 2019-11-27 16:06:37
So, I've got a handy little bit of functionality in place in a jQuery Mobile app that lets the user know if he/she is navigating away from the page, as this will trigger a log off. In simplified form: $(window).bind('beforeunload', function() { return "Navigating away from this page will log you off. Are you sure you want to continue?"; }); $(window).unload(function() { logoff(); }); This works just great in both Safari and Chrome on my laptop. However, it doesn't appear to work on the iPhone simulator. Does the mobile browser included in iOS (I know it's Safari, but presumably a different

How to display onbeforeunload dialog when appropriate?

匆匆过客 提交于 2019-11-27 15:17:07
I've got an editor in javascript on my webpage and I would like to ask user if he/she wants to leave the page even if there are unsaved changes. I know I can add custom message to the "onbeforeunload dialog" this way: window.onbeforeunload = function() { return 'You have unsaved changes!'; } ( Source ) but I want to display the dialog only where there really are some unsaved changes. How to do that? Thanks! You can do something like this: var unsavedChanges = false; window.onbeforeunload = function() { if (unsavedChanges) return 'You have unsaved changes!'; } function makeSomeChange() { // do

jQuery beforeunload custom pop up window for leaving a page

雨燕双飞 提交于 2019-11-27 14:17:22
Hi I would like to customize my pop up for leaving a page, is there any simple way to do that? I am using simple jQuery $(document).ready(function() { var myPopUp = $('#pop-up').css('display', 'block'); $(window).bind('beforeunload', function(){ return 'load my pop up here instead of the default browser message'; }); }); my html for the pop up is which is hidden by default <div id="pop-up"> <h2>Are you sure wanna leave</h2> <a href="#">Leave</a> <a href="#" class="close">Stay</a> </div> Thanks Short answer: You can't . Longer answer: For rather obvious "security" reasons it is very limited