Skip to content

Instantly share code, notes, and snippets.

@TakashiSasaki
Created December 14, 2012 05:32
Show Gist options
  • Save TakashiSasaki/4282903 to your computer and use it in GitHub Desktop.
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.
<!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>
@techsin
Copy link

techsin commented Oct 29, 2018

👍

@OoonaNet
Copy link

Thank you

@timhecker
Copy link

ok!

@k-funk
Copy link

k-funk commented May 6, 2020

window.dispatchEvent( new Event('storage') );

Use this ^. initEvent is deprecated.

@nunisa
Copy link

nunisa commented Oct 20, 2020

👍

@vespaiach
Copy link

thanks

@ggluta
Copy link

ggluta commented Sep 30, 2021

Just what I needed. Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment