onbeforeunload

Cancel onbeforeunload event handler?

≯℡__Kan透↙ 提交于 2019-12-01 02:17:59
I have an onbeforeunload event handler attached to the page which executes every time the page reloads / gets redirected. window.onbeforeunload = function() { console.log("do something");} I do not want this script to run during my tests. I want to disable it in my own test environment. Is there some way to unbind this onbeforeunload event? I am using JavaScript, with jQuery as the framework, so an answer in jQuery would be good. Ibu In javascript functions can be overwritten. You can simply assign it a new purpose: window.onbeforeunload = function () { // blank function do nothing } This will

Is there any way to detect if user pressed “Stay on page” or “Leave page” in beforeunload event?

牧云@^-^@ 提交于 2019-11-30 21:39:09
Is there any way that I could detect in the following code that either user clicked "leave page" or "stay on page" button ? $(window).on('beforeunload', function (){ return "You save some unsaved data, Do you want to leave?"; }); With a few hacks you can at least determine if the user stayed. If the user left the page, there's not much you can do about that : var timeout; $(window).on('beforeunload', function (){ timeout = setTimeout(function() { // user stayed, do stuff here }, 1000); return "You save some unsaved data, Do you want to leave?"; }); FIDDLE iirc, the unload event is not

Internet Explorer calling window.onbeforeunload on window.open and AJAX calls

泪湿孤枕 提交于 2019-11-30 18:43:33
Ok, I have spent a while on this problem and this is what I have gathered: If you make an AJAX call in IE7 and you have a window.onbeforeunload function specified, it calls the onbeforeunload function. If you try to open a new window with window.open WITHOUT disturbing the current window, the onbeforeunload gets called. Does anyone know how to stop this? I even tried setting a variable to TRUE and check that variable in my onbeforeunload function and it still dosent work! I just need to be able to stop the execution of that method for AJAX calls and new window calls. Another and probably

How to collect return value of onbeforeunload

心已入冬 提交于 2019-11-30 18:15:36
I am displaying a warning message if user try to close window without saving the form. window.onbeforeunload = askConfirm; function askConfirm() { // needToConfirm is set to true if any changes are there in the form if (needToConfirm) { return "Your unsaved data will be lost."; } } function call_this_if_user_clicks_on_cancel() { // Bla bla bla // After this user should remain on the same page. } Now I want to call another function when user clicks on Okay . And rest of the functionality should remain same. So how can I collect onbeforeunload return value and call another function ? SOLUTION:

How to access JavaScript href event that triggered beforeunload

主宰稳场 提交于 2019-11-30 17:41:46
问题 I would like to access the event that caused a beforeunload event. Specifically, if I click on a link to another page, I would like to know what the link was in the beforeunload event handler. In this way, I would be perform different actions in the beforeunload event handler according to what the URL was. Eg 'http:' or 'https:' warn user about losing unsaved changes; 'mailto:' or 'skype:' don't warn user because page is not actually going to be unloaded. I am trying to build a good solution

Activating OnBeforeUnload ONLY when field values have changed

时光总嘲笑我的痴心妄想 提交于 2019-11-30 13:41:32
问题 What I'm trying to achieve is to Warn the user of unsaved changes if he/she tries to close a page or navigate away from it without saving first. I've managed to get the OnBeforeUnload() dialog to pop-up... but I don't want it to be displayed at all if the user hasn't modified any field values. For this, I'm using this hidden input field called is_modified that starts with a default value of false and flips to true when any field is edited. I tried to bind the change event to this is_modified

How can I use @HostListener('window:beforeunload') to call a method?

非 Y 不嫁゛ 提交于 2019-11-30 13:29:07
问题 I am trying to call a post method I made myself before my component is unloaded, but it doesn't work. I put a breakpoint on @HostListener and it doesn't break there when I open a different component. I'm using Chrome to debug and I turned on Event Listener Breakpoints for unload and beforeunload, which do break when I open a different component. Am I missing something in my code? import { Component, OnInit, HostListener } from '@angular/core'; Component({ templateUrl: 'new-component.html' })

Distinguish onbeforeunload for File Download vs Page Change

妖精的绣舞 提交于 2019-11-30 10:57:40
I have an onbeforeunload event that's supposed to get triggered any time a user goes to a new page. It works well enough, but I've found that it also gets triggered in Chrome any time a user downloads a file from the page they're on. I'd like to be able to tell if the event is getting fired because it's being triggered by a file download. What's the best way to do that? EDIT: As a clarification, I don't own the site that I'm listening to onbeforeunload on. The event is listened to by a Javascript snippet that's being installed on 3rd party sites. A javascript work around That's the only "clean

Chrome / Firefox doesn't display images in objects shown in beforeunload event

点点圈 提交于 2019-11-30 09:53:48
问题 I'm using jQuery blockUI plugin to show some nifty "loader" on each AJAX call and each URL change. Here is full code responsible for that: var rootPath = document.body.getAttribute("data-root"); $.blockUI.defaults.message = '<h3><img style="margin: 0 5px 5px 0" src="' + rootPath + '/images/process.gif" width="48" height="48" />In progress...</h3>'; $.blockUI.defaults.css.top = '45%'; $(document).ajaxStart($.blockUI).ajaxStop($.unblockUI); $(window).on('beforeunload', function(){$.blockUI();})

Activating OnBeforeUnload ONLY when field values have changed

爱⌒轻易说出口 提交于 2019-11-30 08:21:26
What I'm trying to achieve is to Warn the user of unsaved changes if he/she tries to close a page or navigate away from it without saving first. I've managed to get the OnBeforeUnload() dialog to pop-up... but I don't want it to be displayed at all if the user hasn't modified any field values. For this, I'm using this hidden input field called is_modified that starts with a default value of false and flips to true when any field is edited. I tried to bind the change event to this is_modified field to try and detect for value change... and only then activate OnBeforeUnload. $( '#is_modified' )