Logout all open tabs automatically when user logs out in one of them

前端 未结 4 1280
日久生厌
日久生厌 2020-12-01 09:31

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 logo

4条回答
  •  自闭症患者
    2020-12-01 10:10

    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!

提交回复
热议问题