Detecting FF browser being closed (X) in 2018 - javascript

A requirement from the customer is to immediately detect that the browser is closed and canceling the session opened from it. This calls for detecting the browser closing event with the onbeforeunload and calling there our logout Servlet from a XmlHttpRequest. It may have worked in the past but today it doesn't work anymore.
After hours of googling and testing I could not come up with a decent solution that detects browser closing on Firefox. I can more or less detect it on other browsers but not on FF. Actually I cannot even catch 100% of the times the event of a tab being closed. Sometimes it works, sometimes it doesn't. Frustrating.
Does anyone in 2018 has a working example to share, that effectively detects on new FF browsers the closing event?

Related

Webpage freezes in Safari on both iOS and OSX when the page reloads?

I'm using an iPhone 11 Pro with iOS 15.3.1.
I'm trying to figure out why when visiting my website, Safari is freezing most of the time when the page loads. Sometimes the page loads the first time I visit it, but on reload it basically freezes with no interaction.
When I connect the iPhone to a Mac and use the Safari inspector to connect to the webpage on the iPhone, the inspector is basically blank on all screens. If I go to the "Elements" tab, nothing shows. If I go to the "Console" tab and try to execute some Javascript, nothing happens when I press enter.
On OSX, I can see similar behavior, except I can actually open the inspector before I visit the page. In this case, when it freezes I can see a few elements in the "Elements" tab, but that's about it. Any sort of interaction I try to do in the inspector results in nothing happening.
I'm at kind of a loss. How can the development experience for such a popular OS/Browser be so terrible, and how do I work around the inspector itself not working? I can't replicate this freezing on anything besides Safari.
To be absolutely clear on my question:
What is your process to debug a webpage in Safari when it is in a state where the inspector no longer interacts with the webpage?
I did a broad divide and conquer of my app, commenting out top level code and working my way down uncommenting until I found the exact line that causes the freeze.
It seems calling HTMLInputElement.setSelectionRange() inside an onfocus event handler function caused an infinite synchronous loop in Safari, perhaps Safari synchronously blurs the input and refocuses for some reason when that function is called. And on page reload Safari was trying to focus on an input with that code automatically. That is my guess at least, removing the setSelectionRange() resolves the freezing problem.
Luckily, the freezing was fairly consistent so the divide and conquer approach worked. Unluckily, the inspector really should have done its job and picked up on the fact that the page was hanging and told me which function code execution broke at when Javascript was forced to stop, like Chrome or Firefox would.
If anyone has any better ideas for debugging these kinds of issues in Safari besides the programming equivalent of a short circuit test, I'll gladly accept the answer.

Redirect to a new page using Browser or Tab Close Event in (Chrome, Firefox and IE) All of them

Please do not mark this as duplicate, as this is not being answered completely in any single post. also no recent working solution for latest browsers is found.
My Question is as under, i am using Asp.net C#, and the code should work on client-side preferably.
Is it Possible to Catch Browser/Tab Close Event for Non IE Browsers (ex. Chrome, Firefox) Current Versions [Version 81+ for Chrome (64-bit)], and Execute navigate to logout page.
The code which i tried for Chrome primarily is as below.
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = 'this is cuctom message';
window.location.href = "~/logout/Logout.aspx";
});
Now when i use above code in IE, it gives me message like
the problem which i am facing with above code is.
On Every link click this message is called in IE, along with tab close too, and i want to call this message only on tab or browser close.
for Chrome though, above code is called only on link navigation, but not redirected to logout page, and not on browser close.
I am stuck in this activity, with no way forward, can anyone please help me out with a way towards solution to this issue.

onClick event problems with Internet Explorer 9

I am having sporadic issues with fullCalendar and the click hook in Internet Explorer 9 (this is not an issue in FireFox, Chrome or Opera it seems).
The function I have attached to the click hook when a user clicks an event will only trigger if I first launch the developer console in Internet Explorer and refresh the page. I can then close the console again and use the page as normal, but if I return 24 hours later, I will have to repeat this step to be able to use the functionality again.
My Internet Explorer settings appear fine, as this is not a problem with just this one machine, but also with 5 other machines I have tested this on.
sounds like there is a console.log() included but without any code provided we can only take a guess.

cross-browser Onunload and Onbeforeunload ? (particularly opera 11)

I have a form and I must notice user with alert() on exiting page while there are data in the form that had not been send.
I've read that opera has a lot of problems with this. Opera 11 that is, because I need take into account only last version.
So again, the alert should display on refresh, closing a tab, or closing whole browser.
It would be nice to set event directly to the <form> element that would be launched on anything that leads to destroying this element.
Opera does not yet support onbeforeunload. Opera's onunload support is incomplete - for example, it does not fire when you close a tab, or close Opera. As far as I know, while you can use onunload to show a warning if a user clicks a link to navigate away from a page with an unsaved form, there is no way you can prevent the navigation from happening when the unload event fires.
What I would do here is to listen for the onbeforeunload event, make it work in other browsers and wait for Opera to catch up. Whether it will be in Opera 12 or 13 or 15 I don't know, but when onbeforeunload is implemented it will work like it does in other browsers already.
The Disable Close will not help you yet because Opera does not support window.onbeforeunload at the moment. It will be supported in some future version. For now, Opera 11 doesn't implement it.
onbeforeunload is now supported since Opera 15 !
The JQuery framework is only a condensed Javascript language so it depends entirely on the browser you use, no magic here.

Is there anyway to trap the closing of a web browser?

Two years ago I had a need to trap the closing of a web browser as a javascript event for a web app. At the time I found there was no way doing this that worked for all browsers. IE had an event that I could used, but it was IE specific. I looked into other work arounds, like a heart beat sort of ping to the server, but I didn't like any of them at the time.
Is there anyway currently to trap the closing of a web browser now? Thanks!
You can use the
window.onbeforeunload
javascript event to do this, though this will trap more than just closing the browser. This event will also get fired each time someone tries to navigate to another page, refresh the current page, etc. It's handy if you're trying to do something like warn people of unsaved changes before they leave the current page.
onunload works in IE and Firefox.

Categories

Resources