“Dengarkan perubahan LocalStorage” Kode Jawaban

Dengarkan perubahan LocalStorage

window.addEventListener('storage', () => {
  // When local storage changes, dump the list to
  // the console.
  console.log(JSON.parse(window.localStorage.getItem('sampleList')));
});
Cookie_Wookie_7

JS Dengarkan LocalStorage Change

var originalSetItem = localStorage.setItem;

localStorage.setItem = function(key, value) {
  var event = new Event('itemInserted');

  event.value = value; // Optional..
  event.key = key; // Optional..

  document.dispatchEvent(event);

  originalSetItem.apply(this, arguments);
};

var localStorageSetHandler = function(e) {
  alert('localStorage.set("' + e.key + '", "' + e.value + '") was called');
};

document.addEventListener("itemInserted", localStorageSetHandler, false);

localStorage.setItem('foo', 'bar'); // Pops an alert
Bored Butterfly

Dengarkan perubahan LocalStorage

var originalSetItem = localStorage.setItem; 
localStorage.setItem = function(){
    document.createEvent('Event').initEvent('itemInserted', true, true);
    originalSetItem.apply(this, arguments);
}
The Coding Master

Jawaban yang mirip dengan “Dengarkan perubahan LocalStorage”

Pertanyaan yang mirip dengan “Dengarkan perubahan LocalStorage”

Lebih banyak jawaban terkait untuk “Dengarkan perubahan LocalStorage” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya