我试图按照Safari开发者网站上的示例进行操作.
文档建议添加一个事件监听器,如下所示:
window.addEventListener('storage', storage_handler, false);
Run Code Online (Sandbox Code Playgroud)
然后像这样设置处理函数:
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);
}
Run Code Online (Sandbox Code Playgroud)
但我似乎无法在我的机器(OS X 10.6,Safari 5.01)上使用此代码,也无法在我的iPhone 3GS(iOS 4.02)上使用Safari.
本文提供了一个单独的方法:
window.onload = function() {
...
document.body.setAttribute("onstorage", "handleOnStorage();");
}
function handleOnStorage() {
if (window.event && window.event.key.indexOf("index::") == 0){
$("stats").innerHTML = "";
displayStats();
}
}
Run Code Online (Sandbox Code Playgroud)
但我也没有任何运气.
难道我做错了什么?这是一个错误吗?