Search code examples
typescriptangular7

Restrict the multiple tabs to open in a same browser


In my application, it should not allow the user to open the application in multiple tabs for same browser. I need to implement this for some security purpose. How can I implement this in angular 8?


Solution

  • The shorter version with localStorage and Storage listener

    <script type="text/javascript">
            // Broadcast that you're opening a page.
            localStorage.openpages = Date.now();
            var onLocalStorageEvent = function(e){
                if(e.key == "openpages"){
                    // Listen if anybody else is opening the same page!
                    localStorage.page_available = Date.now();
                }
                if(e.key == "page_available"){
                    alert("One more page already open");
                }
            };
            window.addEventListener('storage', onLocalStorageEvent, false);
    </script>