Prevent 'beforeunload' event being fired on window.location - javascript

I have an analytics service which uses
window.addEventListener('beforeunload', endSession);
to record the user navigating away from the app.
In another part of the app we have downloading functionality, using
window.location = urlOfItemToDownload;
This causes the beforeunload event to trigger, which is undesirable.
There are many downloads in the app so toggling a boolean variable isnt so scalable. Is there any way around it?

Related

Correctly handling the BFCache in react

I'm showing a loading screen (state) before redirecting to an external url (payment provider), since they tend to load for a while.
If the user chooses not to complete the payment and goes history.back() (Gesture, back button, ...) the browser pulls the page before from the BFCache, including the loading state, and the user will be "stuck" loading forever.
How would you suggest handling that? Persisted loads can be detected using the pageshow event, but I'm not sure if that is the react way to handle the case.
Persisted loads should be handled manually. In your case you should handle the
loader state based on the event property persisted from pageshow event handler.

trigger unload event in iframe app when parent application tab closes

I have two apps, App1 and App2.
App2 is embedded as an iframe in App1, so App1 is parent app which embeds App2.
When user closes App1 from browser, I want to trigger an api in App2.
I tried using 'unload' event in App2, but that is not getting triggered when App2 closes (am checking by putting debugger point on event listener function, which gets called when standalone App2 gets closed).
Is there a way to accomplish this? Thanks
Historically trying to make asynchronous requests in unload events has never been reliable
For that reason navigator.sendBeacon(url, data) was created.
It works in the background even after page or tab is gone and helps solve a lot of the reliability problems of using unload events.
With the sendBeacon() method, the data is transmitted asynchronously when the User Agent has an opportunity to do so, without delaying unload or the next navigation.
I've never used it in the disappearing iframe scenario like yours but believe it would solve your issue

Is there a way to prevent someone from closing a web app?

I work in a call center and we use an in-house app for dialing. The app is built using HTML/JavaScript for the front end and MSSQL/Node.js for the back end.
We have a problem where some people close the browser without logging out properly and this is causing data to be lost because the phone call is not being terminated correctly. Is there any way to prevent the user from closing the app using the 'X' button in Chrome?
I have already setup the generic Chrome message that warns of data loss if the browser is closed, but this isn't really doing the job.
I am looking for either some kind of JavaScript code to run in the app or perhaps something externally I can run on the local computer. Also, a Chrome startup command line switch would be perfect for the job too, but from my research, I am not sure one exists.
$(window).on("beforeunload", function() {
return confirm("Do you really want to close?");
})
There is no specific event for capturing browser close event.
You can only capture on unload of the current page.
By this method, it will be effected while refreshing / navigating the current page.
Ref

Page Unload Event in Cordova

I have a cordova app. I that I want do some task like stop the Location service when the user try to leave the page. I have tried the window.beforeunload but it didnt not work. Is there a way i can do this.
If you want to perform an action (do some task) before the user leaves your app, you can use the window.onunload event.

Javascript: user confirmation when leaving single-page application

I would like to make the user confirm that she wants to leave my single-page application. ExtJS 4.1 provides the onWindowUnload in which I would like to plug in a Confirm dialog.
If the user decides to proceed closing the tab/window an Ajax request to the logout URL should tell the server-side that the user has logged out.
Is the above approach possible?
Is it possible to plug the onWindowUnload event in ExtJS MVC controller?
P.S. User leaving single-page app can be any of the following: browser tab close, browser window close, browser back button, browser forward button.
You need to add a listener to the window's onbeforeunload event.
It is a special type of event handler, and you should read the docs closely.
MDN docs: https://developer.mozilla.org/en/DOM/window.onbeforeunload
MS docs: http://msdn.microsoft.com/en-us/library/ie/ms536907(v=vs.85).aspx
Ext's standard way of attaching event handlers DOES NOT WORK† with this particular event.
You need to use the standard addEventListener or attachEvent methods.
Example:
http://jsfiddle.net/NhLQK/9/
Can I use my own dialog? No
Can I display my own message in Firefox? No
Can I use a confirm()? No
Can I make them look the same in different browsers? No
Can I fire an AJAX message before the user closes the browser? Yes,
if it's synchronous:
Ext.Ajax.request({
url:'some url',
async:false,
method:'POST'
});
† it seems to work in Chrome with ext 4.1.1

Categories