Identifying Between Refresh and Close Browser Actions

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



Leave a reply



Submit