I open two pages and after changing the data, why don’t they enter the monitoring and processing events? Please give me some advice. (FireFox 10.0 test)
The code is as follows:
<!DOCTYPE html><html> <head> <meta charset = "utf-8" /> <title>LocalStorage </title> <script type="text/javascript" src="jquery-1.7.1.min.js"></script> <script type="text/javascript"> var db = window.localStorage; if(db) { if(window.addEventListener) { window.addEventListener("db",displayStorageEvent); console.log('设置监听'); } } function displayStorageEvent(e) { console.log('进入事件处理'); console.log('改变的字段是'+e.key); console.log('旧的值是'+e.oldValue); console.log('新的值是'+e.newValue); } function setdata() { if(db) { db.setItem('author', $("#author").val()); } } function showStorage() { var s =''; for(var i=0;i<db.length;i++) { s = s + db.key(i)+ " : " + db.getItem(db.key(i)) + "<br>" } $("#data")[0].innerHTML = s; } </script> </head> <body> <input type="button" value="显示存储数据" onclick="showStorage()"/> <form id ="info"> <label for="author">作者:</label> <input type="text" name="author" id="author" /> <br/> <input type="button" value="设置LocalStorage" onclick="setdata()"/> </form> <P id="data"></P> </body> </html>
This event has never been used
Fainted, did it myself.
window.addEventListener("db",displayStorageEvent); The parameter is not "db"
but
window.addEventListener("storage",displayStorageEvent);
I understood it wrong.
Hey, I am using IE8