I have created Login page, based on localStorage. On loading the page I have checked the value of localStorage. If I opened the web page in more than one tab and then I logout from any one of the tabs, all the remaining pages should logout automatically.
If reload/refresh means it logging out.
Please help me to run a script, when user view the page or say any other way to solve this problem.
localStorage. setItem('logout-event', 'logout' + Math. random()); Every other tab will get it with a listener.
localStorage. removeItem("token_name"); Step-3: Use storage event. It will be fired automatically in all tabs when localStorage will be modified.
Solution 1 While login in page load check the Session["UserName"] value.. if already logged in the some user in the browser.. then redirect default page... if (Session["LoginUserName"] != null) { //redirect to default page.. like Homepage.. }
You can use Storage events to be notified when localStorage values are changed.
function storageChange (event) { if(event.key === 'logged_in') { alert('Logged in: ' + event.newValue) } } window.addEventListener('storage', storageChange, false)
If, for example, one of the tabs logs out:
window.localStorage.setItem('logged_in', false)
Then all other tabs will receive a StorageEvent
, and an alert will appear:
Logged in: false
I hope this answers your question!
"localStorage persists any saved data indefinitely on the user's computer and across browser tabs" Source
This means that if you empty / remove the login data you've set in one tab, the data will be changed in all other tabs as well.
So, if the user logs out, localStorage data changes.
Then, on your tabs, detect when a user changes focus to that tab again using the onfocus
event:
function onFocus(){ //Reload Page if logged out (Check localStorage) window.location.reload(); }; if (/*@cc_on!@*/false) { // check for Internet Explorer document.addEventHandler("focusin", onFocus); } else { window.addEventHandler("focus", onFocus); }
Source
This means you won't be constantly running javascript, or reloading (a possibly large amount of) tabs at the same time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With