2

The following sample works fine in Firefox, but doesn't work at 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>

Here is a kind of text chat. handle_storage fires in the second tap or window of Firefox, but it doesn't fire in any tab or window of Chrome.

How to fix?

Herohtar
  • 5,347
  • 4
  • 31
  • 41
splash27
  • 2,057
  • 6
  • 26
  • 49
  • 1
    How are you accessing the file? This answer (http://stackoverflow.com/questions/19150638/localstorage-event-listener-cannot-fire-in-chrome) indicates that using the `file://` protocol prevents it from firing. – Curtis Mattoon Nov 26 '14 at 14:24
  • possible duplicates of [How can I get an event to fire every time localStorage is updated in Safari 5+?](http://stackoverflow.com/questions/3562929/how-can-i-get-an-event-to-fire-every-time-localstorage-is-updated-in-safari-5), [HTML5 / JS storage event handler](http://stackoverflow.com/questions/3055013/html5-js-storage-event-handler)... – War10ck Nov 26 '14 at 14:25
  • 2
    @Curtis Mattoon, thank you! The `file://` was the reason. – splash27 Nov 26 '14 at 14:34
  • Possible duplicate of [localStorage event listener cannot fire in chrome](https://stackoverflow.com/questions/19150638/localstorage-event-listener-cannot-fire-in-chrome) – Herohtar Jan 22 '18 at 19:49

0 Answers0