/    Sign up×
Community /Pin to ProfileBookmark

Trap changes in sessionStorage?

I am trying to get an alert when any value int sessionStorage is changed. I have found this code, but I cannot get it to work

“`
window.addEventListener(“storage”, function(event) {
if (event.storageArea === sessionStorage) {
alert(“NO alert”)
}
});
“`

I am looking for more like (pseudocode)

“`
window.addEventListener(“storage”, function(event) {
if event.onchange (key)
alert(“Key changed”)
}
});
“`

https://jsfiddle.net/uhbdc0Lg/10/

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 20.2023 — MDN says that the `event` contains key, value and oldValue. Wouldn't this be sufficient?
Copy linkTweet thisAlerts:
@sibertauthorJan 20.2023 — > @Sempervivum#1650085 event contains key, value and oldValue. Wouldn't this be sufficient?

https://jsfiddle.net/4uqv0xc1/1/

?
Copy linkTweet thisAlerts:
@SempervivumJan 20.2023 — I see, doesn't fire. Stackoverflow discusses this issue:

https://stackoverflow.com/questions/35865481/storage-event-not-firing
>The StorageEvent is fired whenever a change is made to the Storage object.

This won't work on the same page that is making the changes — it is really a way for other pages on the domain using the storage to sync any changes that are made
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...