Ok, so if I have a Session Storage variable 'firstname' whose value is 'sayuj' (currently) and suppose if i want to fire a function when the value of 'firstname' variable changes
Example:
window.addEventListener('storage',function()=>{
alert('session storage variable value changed');
});
Yes, you would do that. And inside the callback, you can check which variable has changed by performing a diff.
Although, this pattern is not really stable. I'd suggest if you're working on something serious and you have a UI framework like Angular or React, then use the underlying APIs to help you perform this action. For example, in React, you can use reducers to make any change to the app state, the app state can be stored in the sessionStorage as well. I've used a similar data flow architecture in my recent open-source app, but with localStorage instead of session.
Ok, so if I have a Session Storage variable 'firstname' whose value is 'sayuj' (currently) and suppose if i want to fire a function when the value of 'firstname' variable changes
Example:
window.addEventListener('storage',function()=>{
alert('session storage variable value changed');
});
Will this code work?
Yes, you would do that. And inside the callback, you can check which variable has changed by performing a diff.
Although, this pattern is not really stable. I'd suggest if you're working on something serious and you have a UI framework like Angular or React, then use the underlying APIs to help you perform this action. For example, in React, you can use reducers to make any change to the app state, the app state can be stored in the sessionStorage as well. I've used a similar data flow architecture in my recent open-source app, but with localStorage instead of session.
Yeah, I can do this thing with the help of Reducers, Thanks!😊