TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Inter-window messaging using localStorage

103 pointsby bensummersabout 12 years ago

9 comments

Sephrabout 12 years ago
This isn't abuse, it's what the storage event was specifically made for. Facebook, Google, etc. have been using this event to update common state information between tabs for quite a while.
评论 #5348996 未加载
hobohackerabout 12 years ago
The primary caveat with this approach is race conditions due to lack of locking. Note the "Issues" section in <a href="http://www.w3.org/TR/webstorage/" rel="nofollow">http://www.w3.org/TR/webstorage/</a> which says:<p>""" The use of the storage mutex to avoid race conditions is currently considered by certain implementors to be too high a performance burden, to the point where allowing data corruption is considered preferable. Alternatives that do not require a user-agent-wide per-origin script lock are eagerly sought after. If reviewers have any suggestions, they are urged to send them to the addresses given in the previous section.<p>More details regarding this issue are available in these e-mails (as well as numerous others):<p><a href="http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-September/023059.html" rel="nofollow">http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-Sep...</a> <a href="http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-December/024277.html" rel="nofollow">http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-Dec...</a> """<p>So as long as you don't need transactions/locks across the shared state, this might work. Of course, transactions/locks are often necessary.
tilesabout 12 years ago
If you want to use localStorage atomically, I found this writeup and code on a LockableStorage interface to be very thorough: <a href="http://balpha.de/2012/03/javascript-concurrency-and-locking-the-html5-localstorage/" rel="nofollow">http://balpha.de/2012/03/javascript-concurrency-and-locking-...</a>
评论 #5349341 未加载
评论 #5349038 未加载
nmjenkinsabout 12 years ago
This technique does indeed work very well. I wrote about it last year, describing how we use it at FastMail to share a single push events connection between all open tabs: <a href="http://blog.fastmail.fm/2012/11/26/inter-tab-communication-using-local-storage/" rel="nofollow">http://blog.fastmail.fm/2012/11/26/inter-tab-communication-u...</a>
jdavidabout 12 years ago
This method may allow message passing between tabs, and windows, but it does not do so between iFrames. Two iFrames on the tab will not trigger storage events on each other.<p>In my opinion this is a bug that should be fixed,<p><a href="https://code.google.com/p/chromium/issues/detail?id=177342" rel="nofollow">https://code.google.com/p/chromium/issues/detail?id=177342</a>
STHaydenabout 12 years ago
As far as I can tell this does not work cross protocol from http to https. One of the thing I use postMessage for the most is messaging between those two protocols.
atarianabout 12 years ago
I was attempting to do something similar, but needed to support IE7 which doesn't support localStorage. You could probably rely on userData as a fallback though.
btiplingabout 12 years ago
localStorage doesn't clear when the browser exits so it's a security issue in some cases. Look at sessionStorage instead in that case.
评论 #5349212 未加载
评论 #5349292 未加载
评论 #5349239 未加载
alxndrabout 12 years ago
Great idea.<p>Doesn't seem to work in Chrome on iOS?
评论 #5349247 未加载
评论 #5349559 未加载