在使用 Storage 进行存取操作的同时,如果需要对存取操作进行监听,可以使用 HTML5 Web Storage api 内置的事件监听器对数据进行监控。只要 Storage 存储的数据有任何变动,Storage 监听器都能捕获。
interface Storage : Event{ readonly attribute DOMString key; readonly attribute DOMString? oldValue; readonly attribute DOMString? newValue; readonly attribute DOMString url; readonly attribute Storage? storageArea; void initStorageEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in DOMString keyArg, in DOMString oldValueArg, in DOMString newValueArg, in DOMString urlArg, in Storage storageAreaArg); };
window.addEventlistener("storage",showStorageEvent,true); function showStorageEvent(e){ console.log(e) }
接下来,就到了你自测的时间了!Go!