JavaScript

Automatically Logout From All Open Tabs When User Logout In Any Tab Using JavaScript

In this blog, you will learn how to automatically log out from all open tabs when the user logout in any tab using javascript.

On websites, When the users open the websites in multiple tabs and then log out from any of them, the active tab refreshed and shows the login page. But the rest tabs are remaining the same, Right?. But now we have a browser “storage” event that can send a signal from one tab to other tabs.

Let’s create a click event for the logout button. it will set localstorge in other words attach storage events.

$(document).ready(function () {
            if (window.localStorage) {
                $('#btnLog').on('click', function () {
                    localStorage.setItem("event-logout", 'logout-' + Math.random());
                    window.location = '/Account/Logout';
                    return true;
                });
            }
});

set listener for same

window.addEventListener('storage', function (event) {
            if (event.key == "event-logout") {
                window.location = '/Account/Logout';
            }
        }, false);

And that’s it.

I hope you guys found something useful. Please give your valuable feedback/comments/questions about this article. Please let me know how you like and understand this article and how I could improve it.

Faisal Pathan

Faisal Pathan is a founder of TheCodeHubs, .NET Project Manager/Team Leader, and C# Corner MVP. He has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET Core, ASP.NET MVC, AngularJS, Angular, React, NodeJS, Amazon S3, Web API, EPPlus, Amazon MWS, eBay Integration, SQL, Entity Framework, JavaScript, eCommerce Integration like Walmart, Tanga, Newegg, Group-on Store, etc. and Windows services.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago