This is actually continuation of my last question. I still face problem since when i refresh browser, either unload or beforeunload also fired which I only want them when browser closed. Just crossed on my mind to use window.outerHeight or window.innerHeight but still can't figure out how to do this. Any suggestion/snippet? Thanks in advance.
Asked
Active
Viewed 8,916 times
2
-
3As far as the server or browser is concerned, a close and a navigation away from the page are synonymous. – Brad Christie Dec 28 '12 at 00:18
-
@BradChristie: If it's synonymous then there'll be no chance to isolate unload/beforeunload event only for closing browser? – Doni Andri Cahyono Dec 28 '12 at 00:33
-
Correct assumption. You can catch a revisit because the server will have a new action to execute, but you can't catch the absense of that (closing the window) if nothing ever happened. that's why websites use "time elapsed since last visit" to determine who's "still online". – Brad Christie Dec 28 '12 at 00:36
-
@BradChristie: So, how can i distinguish between unload/beforeunload for refresh and closing browser? Or do you have a suggestion how to solve my case? – Doni Andri Cahyono Dec 28 '12 at 00:41
-
1@DoniAndriCahyono you can't distinguish between them. – DA. Dec 28 '12 at 00:49
-
@DA: Any idea how to avoid logout when browser refreshed? Since I made it that way ... – Doni Andri Cahyono Dec 28 '12 at 01:08
-
Why do you want to log them out on page unload? I wouldn't want that as a user. If you have to do something similar, the typical solution is a session time out. – DA. Dec 28 '12 at 01:53
-
@DA: I want to prevent user forgot to logout when s/he closed the browser. – Doni Andri Cahyono Dec 28 '12 at 02:41
-
I've never seen a website behave that way. As it's an unexpected behavior, I'd strongly suggest not trying to do that. You also run into other issues such as people that have your site open in multiple tabs, for instance. Use session timeout instead. – DA. Dec 28 '12 at 02:50
1 Answers
3
As far as I know it is impossible to detect a browser close separately from a browser refresh, because the browser does not provide the webpage (window) with that information. As far as your page and its Javascript code are concerned, the two are one and the same.
Since it's browser info that you need, the only way to get it is to use something that has access to that info, ie. a browser plug-in. But then you'll need to make a plug-in for each browser, and get every user to install it. Since I highly doubt you'll find this worthwhile, the real answer to your question (as frustrating as it may be) is: stop trying to detect refreshes vs. closes and move on.

machineghost
- 33,529
- 30
- 159
- 234
-
Actually, I just realized that what I said is not entirely true: you *could* use local storage to keep track of when the last page load was (`var localStorage.lastLoad = new Date()`) and then check that time on page load; if `new Date()` is close enough to `localStorage.lastLoad` you could infer that a reload happened. But that still won't let you do anything with that info unless they do a reload, so this probably isn't very helpful. – machineghost Dec 28 '12 at 00:51
-
could you please give more exact snippet? I still don't get your point. Thanks in advance – Doni Andri Cahyono Dec 28 '12 at 02:43
-
My comment was more of an aside to try and be as factually accurate as possible; it wasn't a real solution proposal. It *won't* tell you when someone leaves your page (ie. onBeforeUnload) whether they're reloading, going to a different page, or closing the browser entirely. All it will tell you is, when the page first loads, did the user just refresh or come to the page for the first time (and even then it's only a guess). – machineghost Dec 28 '12 at 17:47
-
1If you wanted that info though (some future reader might) the basic technique is: 1) onLoad store (either with a cookie or with `localStorage`) the current time under a "lastVisited" key, 2) just before you do that, onLoad, check your cookie or localStore to see if there's a "lastVisited" time 3) if the lastVisited time is less than X minutes (10?) ago from the current time, assume the user reloaded the page. – machineghost Dec 28 '12 at 17:50
-
2In other words, write a JS function "within!0Minutes" that tells you whether two dates are within 10 minuts of each other, then onLoad do `if (withinTenMinutes(new Date(), localStorage["lastVisited"])) doReloadStuff(); else doFreshLoadStuff(); localStorage['lastVisited'] = new Date();` – machineghost Dec 28 '12 at 17:51