Skip to content

Instantly share code, notes, and snippets.

@TakashiSasaki
Created December 14, 2012 05:32
Show Gist options
  • Select an option

  • Save TakashiSasaki/4282903 to your computer and use it in GitHub Desktop.

Select an option

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>
@hasanhameed07
Copy link
Copy Markdown

This shorthand version also works the same:
window.dispatchEvent( new Event('storage') );

@techsin
Copy link
Copy Markdown

techsin commented Oct 29, 2018

๐Ÿ‘

@OoonaNet
Copy link
Copy Markdown

Thank you

@timhecker
Copy link
Copy Markdown

ok!

@k-funk
Copy link
Copy Markdown

k-funk commented May 6, 2020

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

Use this ^. initEvent is deprecated.

@nunisa
Copy link
Copy Markdown

nunisa commented Oct 20, 2020

๐Ÿ‘

@vespaiach
Copy link
Copy Markdown

thanks

@ggluta
Copy link
Copy Markdown

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