Last active
June 18, 2019 12:56
-
-
Save nwrox/eb188d183e02146969deb05882a7e128 to your computer and use it in GitHub Desktop.
Share memory or session storage in a multi tab environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://blog.guya.net/2015/06/12/sharing-sessionstorage-between-tabs-for-secure-multi-tab-authentication/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Removed IE support in this demo for the sake of simplicity | |
(function() { | |
window.memoryStorage = {}; | |
function isEmpty(o) { | |
for (var i in o) { | |
return false; | |
} | |
return true; | |
}; | |
if (isEmpty(memoryStorage)) { | |
// Ask other tabs for memoryStorage | |
localStorage.setItem('getSessionStorage', Date.now()); | |
}; | |
window.addEventListener('storage', function(event) { | |
//console.log('storage event', event); | |
if (event.key == 'getSessionStorage') { | |
// Some tab asked for the memoryStorage -> send it | |
localStorage.setItem('sessionStorage', JSON.stringify(memoryStorage)); | |
localStorage.removeItem('sessionStorage'); | |
} else if (event.key == 'sessionStorage' && isEmpty(memoryStorage)) { | |
// sessionStorage is empty -> fill it | |
var data = JSON.parse(event.newValue), | |
value; | |
for (key in data) { | |
memoryStorage[key] = data[key]; | |
} | |
showSessionStorage(); | |
} | |
}); | |
window.onbeforeunload = function() { | |
//sessionStorage.clear(); | |
}; | |
/* This code is only for the UI in the demo, it's not part of the sulotion */ | |
var el; | |
function showSessionStorage() { | |
el.innerHTML = !isEmpty(memoryStorage) ? JSON.stringify(memoryStorage) : 'memoryStorage is empty'; | |
} | |
window.addEventListener('load', function() { | |
el = document.getElementById('stData'); | |
showSessionStorage(); | |
document.getElementById('btnSet').addEventListener('click', function() { | |
memoryStorage.token = '123456789'; | |
showSessionStorage(); | |
}) | |
document.getElementById('btnClear').addEventListener('click', function() { | |
memoryStorage = {}; | |
showSessionStorage(); | |
}) | |
}) | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Removed IE support in this demo for the sake of simplicity | |
(function() { | |
if (!sessionStorage.length) { | |
// Ask other tabs for session storage | |
localStorage.setItem('getSessionStorage', Date.now()); | |
}; | |
window.addEventListener('storage', function(event) { | |
//console.log('storage event', event); | |
if (event.key == 'getSessionStorage') { | |
// Some tab asked for the sessionStorage -> send it | |
localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage)); | |
localStorage.removeItem('sessionStorage'); | |
} else if (event.key == 'sessionStorage' && !sessionStorage.length) { | |
// sessionStorage is empty -> fill it | |
var data = JSON.parse(event.newValue), | |
value; | |
for (key in data) { | |
sessionStorage.setItem(key, data[key]); | |
} | |
showSessionStorage(); | |
} | |
}); | |
window.onbeforeunload = function() { | |
//sessionStorage.clear(); | |
}; | |
/* This code is only for the UI in the demo, it's not part of the sulotion */ | |
var el; | |
function showSessionStorage() { | |
el.innerHTML = sessionStorage.length ? JSON.stringify(sessionStorage) : 'sessionStorage is empty'; | |
} | |
window.addEventListener('load', function() { | |
el = document.getElementById('stData'); | |
showSessionStorage(); | |
document.getElementById('btnSet').addEventListener('click', function() { | |
sessionStorage.setItem('token', '123456789'); | |
showSessionStorage(); | |
}) | |
document.getElementById('btnClear').addEventListener('click', function() { | |
sessionStorage.clear(); | |
showSessionStorage(); | |
}) | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment