“存储事件”在Chrome中无效

时间:2014-11-26 14:17:33

标签: javascript google-chrome local-storage

以下示例在Firefox中运行良好,但在Chrome中无效。

<!doctype html>
<meta charset="utf-8">
<textarea id="out" cols="80" rows="20"></textarea>
<br>
<p>
Text to send: 
<input id="in" type="text" size="60">
<button id="btnSend">Send</button>
<button id="btnClear">Clear</button>
</p>
<script>
btnClear = document.getElementById("btnClear");
btnSend = document.getElementById("btnSend");
inp = document.getElementById("in");
out = document.getElementById("out");

var handle_storage = function (event) {
    if (event.key == "message") { out.value += event.newValue + "\n";}
};

window.addEventListener("storage", handle_storage, false);

btnSend.onclick = function() {
    localStorage.setItem('message', inp.value);
    out.value += inp.value + "\n";
    inp.value = "";
};
btnClear.onclick = function() {
    inp.value = "";
    out.value = "";
};
</script>

这是一种文字聊天。 handle_storage在Firefox的第二个点击或窗口中触发,但它不会在Chrome的任何标签或窗口中触发。

如何解决?

0 个答案:

没有答案