js listen localstorage change

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

4
10

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

4 (10 Votes)
0
4.57
7
Rajarshi 125 points

                                    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

4.57 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
js local storage change listener ljs listener on change in localstorage listen to change in local storage javascript local storage listen to updates listen local storage value change event javascript web listen to localstorage changes handle localstorage change js on localstorage change local storage change listener listen for localstorage change javascript listen for localstorage changes JAVASCRIPT LISTEN for localstorage change how to listen to value changes in localstorage localstorage change listener localStorage value change event window on localstorage change modify localstorage js watch for localstorage change change in local storage event listener localstorage event change get localstorage change javascript localstorage change event example js localstorage on change event localstorage change how to get local storage changed in javascript javascript localstorage change event watch localstorage changes javascript how to watch for changes in localstorage how to listen for localStorage value changes localstorage change event listen to localstorage changes angular localstorage watch for changes listen localstorage change javascript watch for change in localstorage localstorage on change javascript event to listen localstorage changes js events on localstorage change listyen for local storage changes subscibe to storage chanages javascript listen to localstorage change js listen when storage changes html listen to storage change when storage is changed javascript google chrome watch localstorage same window react listen to local storage change javascript observe localstorage same window javascript observe localstorage listener for localstorage change javascript localstorage listen for changes javascript listen localstorage change listen to localstorage changes listen to changes in localstorage not able to listen to changes in local storafe js listen localstorage change listen to local storage changes javascript login page listen change localstorage change listn to changes in sessionstorage javascript listen to dom add children vue listen listen dom add element
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source