73
How to detect localStorage updates between browser tabs or window
2021-05-05 - JavaScript
1// JS Tip of the Day
2// How to detect localStorage updates between browser tabs or window
3
4// Browser Tab or Window - http://example.com
5window.addEventListener('storage', (event) => { // or you may use window.onstorage
6 console.log(event.key, event.newValue, event.oldValue, event.url);
7 // Hero, Batman, null, http://example.com/heroes
8});
9
10
11// Another Browser Tab or Window - http://example.com/heroes
12localStorage.setItem('hero', 'Batman');
#JStipoftheday