我试图关注the examples on the Safari Developer Site。每次在Safari 5+中更新localStorage时,如何获得一个事件触发?
文档建议添加事件监听器,如下:
window.addEventListener('storage', storage_handler, false);
,然后设置你的处理函数是这样的:
function storage_handler(evt)
{
alert('The modified key was '+evt.key);
alert('The original value was '+evt.oldValue);
alert('The new value is '+evt.newValue);
alert('The URL of the page that made the change was '+evt.url);
alert('The window where the change was made was '+evt.source);
}
但我似乎无法获得此代码工作的我的机器(OS X 10.6,Safari 5.01)以及我的iPhone 3GS(iOS 4.02)上的Safari。
This article提供了一个单独的方法:
window.onload = function() {
...
document.body.setAttribute("onstorage", "handleOnStorage();");
}
function handleOnStorage() {
if (window.event && window.event.key.indexOf("index::") == 0){
$("stats").innerHTML = "";
displayStats();
}
}
但我还没有与任何运气无论是。
我做错了什么?这是一个错误?
我固定在你对你的问题的联系;一旦你得到更多的代表,你应该能够很快添加更多的链接。可悲的是,我不知道你的问题的答案。希望别人能帮助! – 2010-08-25 05:09:43
其代码片段的一个很好的演示已经可以[在这里](http://html5demos.com/storage-events)了解这个概念以及它是如何工作的。 http://html5demos.com/storage-events – msoliman 2015-09-19 21:33:12
仍然有这个链接,可能也有助于理解sessionStorage很好https://www.nczonline.net/blog/2009/07/21/introduction-to-sessionstorage/ – msoliman 2015-09-20 11:14:59