Skip to content

Commit 34c978a

Browse files
committed
added storage event demo
1 parent b8f88cd commit 34c978a

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

demos.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
[
2+
{
3+
"desc": "Storage events",
4+
"url": "storage-events",
5+
"tags": "storage",
6+
"support": {
7+
"live": "chrome safari opera firefox"
8+
}
9+
},
210
{
311
"desc": "dataset (data-* attributes)",
412
"url": "dataset",
@@ -15,8 +23,7 @@
1523
"notes": "Uses onpopstate event",
1624
"tags": "history",
1725
"support": {
18-
"live": "chrome safari",
19-
"nightly": "firefox"
26+
"live": "chrome safari firefox"
2027
},
2128
"test": "Modernizr.history"
2229
},

demos/storage-events.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<title>Storage Events</title>
2+
<style>
3+
article div {
4+
margin: 10px 0;
5+
}
6+
7+
label {
8+
float: left;
9+
display: block;
10+
width: 125px;
11+
line-height: 32px;
12+
}
13+
14+
#fromEvent {
15+
border: 1px solid #ccc;
16+
padding: 10px;
17+
}
18+
</style>
19+
<article>
20+
<section>
21+
<p><strong>Directions:</strong> open multiple windows or tabs with <a href="/storage-events">this demo</a> and change the text below.</p>
22+
<p>The <code>storage</code> event triggers on the "blurred" windows, giving us a way to communicate across windows using <code>localStorage</code>.</p>
23+
<div>
24+
<p><label for="data">Your test data:</label> <input type="text" name="data" value="" placeholder="change me" id="data" /> <small>(this is only echoed on <em>other</em> windows)</small></p>
25+
<p id="fromEvent">Waiting for data via <code>storage</code> event...</p>
26+
</div>
27+
</section>
28+
</article>
29+
<script>
30+
31+
var dataInput = document.getElementById('data'),
32+
output = document.getElementById('fromEvent');
33+
34+
addEvent(window, 'storage', function (event) {
35+
if (event.key == 'storage-event-test') {
36+
output.innerHTML = event.newValue;
37+
}
38+
});
39+
40+
addEvent(dataInput, 'keyup', function () {
41+
localStorage.setItem('storage-event-test', this.value);
42+
});
43+
44+
</script>

0 commit comments

Comments
 (0)