I'm actually trying to find some solution to see if the tab of the site was closed and store it on localStorage
, I'm using onunload
and onbeforeunload
for this scenario but the problem is that if I refresh/reload (ctrl+r
) or press the f5
button the value will set to true.
Asked
Active
Viewed 46 times
0

Matheus Lacerda
- 5,983
- 11
- 29
- 45

kim nicole sabordo
- 108
- 1
- 11
-
yes, because f5 unloads then loads ... there's no way to distinguish – Jaromanda X Jun 14 '18 at 04:10
-
module? api? you've totally lost me – Jaromanda X Jun 14 '18 at 04:12
-
[This post](https://stackoverflow.com/questions/5004978/check-if-page-gets-reloaded-or-refreshed-in-javascript) might be helpful ..? – Teemu Jun 14 '18 at 04:18
-
@JaromandaX https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigation Though not widely supported, yet. – Teemu Jun 14 '18 at 04:25
1 Answers
0
You could un-set the value by determining if the page was indeed refreshed. When the site is closed, save the time at which the site was closed in local storage. When the page loads, compare the current time to the saved time and if it's "too recent", set the value to false.

Brett Commandeur
- 540
- 3
- 7
-
[This](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigation) is much easier ... – Teemu Jun 14 '18 at 04:21
-
It sure is! I trusted the above comment about "no way to distinguish". Turns out there is. – Brett Commandeur Jun 14 '18 at 04:24
-
1indeed, @BrettCommandeur, sorry for misleading you, I'm sure the *not widely supported* solution is better than nothing – Jaromanda X Jun 14 '18 at 04:33