How to distinguish between refresh (by F5/refresh button) and close browser?
You might want to use Cookies/window.sessionStorage.
You can set a cookie without an explicit expiration date so that it is available only for the current session (which is valid till user closes the browser window).
You can also use sessionStorage object, it stores the data for only one session. The data is deleted when the user closes the specific browser tab.
You can follow this appraoch :
1.Create a cookie when user first visits the page document.cookie = "userloggedin=true";
OR set sessionStorage.userLoggedIn = true;
2.Cookie will be available after refresh/closing and reopening tab , if cookie is not present then it means user closed the window and re-opened it.
Similarly sessionStorage data will be deleted after user closes browser tab
Hope this helps !
Differentiate between refresh and tab close browser in node.js
There is no way to distinguish between browser tab close and browser tab refresh in node.js.
But i find a way via javascript. Here it is
How to differentiate browser close and refresh using angularjs?
If you do not need this to work across different windows/tabs, then you should use sessionStorage
instead of localStorage
.
Where what you store into localStorage
is “permanent”, sessionStorage
stores values only until the window/tab is closed, similar to a “session cookie”, that is set without any lifetime – that will live only until the browser is closed (but will be available across different tabs when your site is open in more than one.)
A few more details can be found in these questions:
What is the difference between localStorage, sessionStorage, session and cookies?
HTML5 Local storage vs. Session storage
Related Topics
"Innerhtml += ..." VS "Appendchild(Txtnode)"
How to Send JSON Instead of a Query String with $.Ajax
JavaScript Regex: How to Put a Variable Inside a Regular Expression
Is There a Null-Coalescing (Elvis) Operator or Safe Navigation Operator in JavaScript
Format Numbers in JavaScript Similar to C#
I Know That Callback Function Runs Asynchronously, But Why
Es6 Destructuring Function Parameter - Naming Root Object
Why Does Firebase Lose Reference Outside the Once() Function
Does React Keep the Order for State Updates
Passing Environment-Dependent Variables in Webpack
Window.Location.Href and Window.Open () Methods in JavaScript
How to Filter an Array from All Elements of Another Array
What Does = +_ Mean in JavaScript
How to Redirect with JavaScript