Skip to content

Instantly share code, notes, and snippets.

@ishukshin
Created February 11, 2017 20:38
Show Gist options
  • Save ishukshin/0bbf783b28f458c1be89874c18d1fc30 to your computer and use it in GitHub Desktop.
Save ishukshin/0bbf783b28f458c1be89874c18d1fc30 to your computer and use it in GitHub Desktop.
<html>
<head>
<style>
#console{
border:1px solid lightgreen;
padding:10px;
}
</style>
</head>
<body>
<div id="console">
open this page in many tabs and press logout in one of them!
</div>
<button type=button id="logout">Logout</button>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script>
var addEvent = (function () {
if (document.addEventListener) {
return function (el, type, fn) {
if (el && el.nodeName || el === window) {
el.addEventListener(type, fn, false);
} else if (el && el.length) {
for (var i = 0; i < el.length; i++) {
addEvent(el[i], type, fn);
}
}
};
} else {
return function (el, type, fn) {
if (el && el.nodeName || el === window) {
el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
} else if (el && el.length) {
for (var i = 0; i < el.length; i++) {
addEvent(el[i], type, fn);
}
}
};
}
})();
// listen to storage event
addEvent(window, 'storage', function (event) {
alert('changed to ' + localStorage.getItem('logout-event'));
// do what you want on logout-event
if (event.key == 'logout-event') {
$('#console').html('Received logout event! Insert logout script here.');
// window.location = "logout.php";
}
});
$(document).ready(function(){
$('#logout').on('click', function(){
alert('send logout');
// change logout-event and therefore send an event
localStorage.setItem('logout-event', 'logout');
alert('changed to ' + localStorage.getItem('logout-event'));
return true;
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment