Created
December 14, 2012 05:32
-
-
Save TakashiSasaki/4282903 to your computer and use it in GitHub Desktop.
HTML5 StorageEvent does not fire "storage" event in the window that changes the state of the local storage.
This sample shows how to fire self-"storage" event.
This file contains 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
<!DOCTYPE html> | |
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta charset="utf-8" /> | |
<title></title> | |
</head> | |
<body> | |
<script> | |
"use strict"; | |
var storage = window.localStorage; | |
storage.setItem("key1", "value1"); | |
alert(storage.getItem("key1")); | |
addEventListener("storage", function () { alert("storage event was fired"); }); | |
var event = document.createEvent("Event"); | |
event.initEvent("storage", true, true); | |
window.dispatchEvent(event); | |
</script> | |
</body> | |
</html> |
👍
Thank you
ok!
window.dispatchEvent( new Event('storage') );
Use this ^. initEvent
is deprecated.
👍
thanks
Just what I needed. Thx!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This shorthand version also works the same:
window.dispatchEvent( new Event('storage') );