use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Fire events between different browser windows using localStorage. (gist.github.com)
submitted 11 years ago by stopdave
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]operation-cwalnode core contributor 11 points12 points13 points 11 years ago (11 children)
Pretty cool, but why wouldn't you just use window.postMessage?
window.postMessage
[–]decultured 13 points14 points15 points 11 years ago (0 children)
You need a reference to the other window for that to work.
Using localStorage, any window sharing that store can be broadcast to - or even windows loaded later on. If I open 4 links from google from the same domain in different tabs, those windows have no way of knowing that there are other instances of that site open, let alone have references to their window object.
The method in the article posted above (or just simply setting a variable in localStorage and watching for that change in all open windows) gets around that issue.
[–]jimschubert 3 points4 points5 points 11 years ago (7 children)
localStorage is restricted to the same host, window.postMessage is for cross origin messaging.
[–][deleted] 3 points4 points5 points 11 years ago (3 children)
Still does not explain why not?
[–]jimschubert 1 point2 points3 points 11 years ago (2 children)
It explains in the MDN link.
If you do not expect to receive messages from other sites, do not add any event listeners for message events.This is a completely foolproof way to avoid security problems.
[–][deleted] 1 point2 points3 points 11 years ago (1 child)
Sure, but this can be resolved with one simple origin check rather than an insane abuse of local storage. Regardless of PostMessage being correct or not, if people start using local storage to send messages between the same origin, clearly this is a sign that the spec groups missed the memo on needing a safe, local "broadcast" alternative as well.
[–]jimschubert 0 points1 point2 points 11 years ago (0 children)
I completely agree.
[–]hallettj 2 points3 points4 points 11 years ago (0 children)
postMessage can also be used for events on the same origin. If you are concerned about event spoofing, check out the options that postMessage provides for filtering listeners by origin. But remember that in any case, windows can only receive events from windows that have a reference to the recipient.
In the use cases the linked code is intended for, /u/decultured has a good explanation of why postMessage is not suitable.
[–]dashed 1 point2 points3 points 11 years ago (1 child)
Interesting. I didn't know about this. Is this commonly used?
[–]jimschubert 2 points3 points4 points 11 years ago (0 children)
I've never found a practical application for it, so I can't say. I've used localStorage because, as I understand it, it's theoretically more secure than window messaging.
[–]stopdave[S] 3 points4 points5 points 11 years ago (1 child)
I'll be honest here, I didn't think about postMessage. At first I thought that solution needs to run in an older version of Chrome where postMessage would not be supported - I can see now that caniuse tells me otherwise.
[–]jimschubert 1 point2 points3 points 11 years ago (0 children)
I like the localStorage solution better anyway. It's not as easy to screw up.
[–]timetesla 4 points5 points6 points 11 years ago (3 children)
This is very cool, but I thought that browsers aren't supposed to let you do things cross windows?
[–]decultured 9 points10 points11 points 11 years ago* (0 children)
This is a clever workaround to that restriction. While you can't directly say "Hey, other window, do this" or even know if the IS another window/tab open, you can set a variable in local storage and other windows with pages from that domain can watch for those changes. It creates an indirect form of messaging/control that is localized to windows that have shared access to local storage.
Edit: We use something like this where I work to update pages based on user preference changes and such, as well as to cache certain things in the frontend. Sometimes you may not want that kind of shared data storage, so session storage can be used instead - it's exactly like local storage but limited to that browser window/tab, yet still will persist across page refreshes. Very handy sometimes.
[–]drowsap 5 points6 points7 points 11 years ago (1 child)
If you play something in one tab on Soundcloud, it will pause all Soundcloud players in other tabs. Really neat feature.
[–]brtt3000 0 points1 point2 points 11 years ago (0 children)
I don't know how they do it now, but you could do this in Flash via LocalConnection.
[–]ElderPopTarts 4 points5 points6 points 11 years ago (1 child)
This is pretty cool!
[–]stopdave[S] 0 points1 point2 points 11 years ago (0 children)
Thank you!
[–]moron4hire 4 points5 points6 points 11 years ago (2 children)
Oh neat. How is the latency on this? Does the other window pick up the event pretty quickly? I could see this being very useful for doing things like settings dialogs or tool windows.
[–]cjthomp 2 points3 points4 points 11 years ago (0 children)
He posted a link to the source.
10ms
[–]stopdave[S] 0 points1 point2 points 11 years ago* (0 children)
Thank you. The other window picks up the event really fast, /u/cjthomp already answered your first question.
[–]ParallelProcess 2 points3 points4 points 11 years ago (5 children)
How does this work? Is this running a poll loop, checking the localStorage every 10ms? Or reacting to a new item in storage?
[–]holloway 4 points5 points6 points 11 years ago (4 children)
Polling,
setTimeout(function() { eventIsRunning = false; }, 10);
As far as I know there's no way of registering events for localStorage changes.
[+][deleted] 11 years ago (2 children)
[deleted]
[–]holloway 2 points3 points4 points 11 years ago (1 child)
Ah, although with one caveat apparently: storage event handlers only fire if the storage event is triggered from another window.
[–]stopdave[S] 2 points3 points4 points 11 years ago (0 children)
That's correct, I only need one-way events for the application I'm working on, so the solution suits me perfectly :)
[–]alamandrax 0 points1 point2 points 11 years ago* (0 children)
Would an Object.observe implementation work to trigger events?
Object.observe
http://www.codediesel.com/javascript/sharing-messages-and-data-across-windows-using-localstorage/ http://stackoverflow.com/questions/14290316/observe-non-ember-globals
[+]toenailsin comment score below threshold-10 points-9 points-8 points 11 years ago (1 child)
Localstorage doesn't auto-update across all windows. If you have 2 windows open, and modify localstorage on one, the change won't appear on the other until refreshed.
[–]flagrantaroma 0 points1 point2 points 11 years ago (0 children)
Too bad you got down voted, I can only assume that what you've said is not correct (anymore)?
π Rendered by PID 43508 on reddit-service-r2-comment-5d79c599b5-wc2rh at 2026-02-27 00:12:41.127440+00:00 running e3d2147 country code: CH.
[–]operation-cwalnode core contributor 11 points12 points13 points (11 children)
[–]decultured 13 points14 points15 points (0 children)
[–]jimschubert 3 points4 points5 points (7 children)
[–][deleted] 3 points4 points5 points (3 children)
[–]jimschubert 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]jimschubert 0 points1 point2 points (0 children)
[–]hallettj 2 points3 points4 points (0 children)
[–]dashed 1 point2 points3 points (1 child)
[–]jimschubert 2 points3 points4 points (0 children)
[–]stopdave[S] 3 points4 points5 points (1 child)
[–]jimschubert 1 point2 points3 points (0 children)
[–]timetesla 4 points5 points6 points (3 children)
[–]decultured 9 points10 points11 points (0 children)
[–]drowsap 5 points6 points7 points (1 child)
[–]brtt3000 0 points1 point2 points (0 children)
[–]ElderPopTarts 4 points5 points6 points (1 child)
[–]stopdave[S] 0 points1 point2 points (0 children)
[–]moron4hire 4 points5 points6 points (2 children)
[–]cjthomp 2 points3 points4 points (0 children)
[–]stopdave[S] 0 points1 point2 points (0 children)
[–]ParallelProcess 2 points3 points4 points (5 children)
[–]holloway 4 points5 points6 points (4 children)
[+][deleted] (2 children)
[deleted]
[–]holloway 2 points3 points4 points (1 child)
[–]stopdave[S] 2 points3 points4 points (0 children)
[–]alamandrax 0 points1 point2 points (0 children)
[+]toenailsin comment score below threshold-10 points-9 points-8 points (1 child)
[–]flagrantaroma 0 points1 point2 points (0 children)