I want this function to run not with the onClick event but when something is stored in the Session Storage. The eventListener did not work.
function mobileHandler(event) {
event.preventDefault();
// Get the selectedNodes array from sessionStorage
const selectedNodes = JSON.parse(sessionStorage.getItem('selectedNodes'));
// If no selectedNodes, return early
if (!selectedNodes) return;
// Loop through each selected node and add it to the chart
selectedNodes.forEach((nodeData) => {
console.log('Adding node:', nodeData);
addNodeData(nodeData);
});
// Clear the selectedNodes from sessionStorage
sessionStorage.removeItem('selectedNodes');
}
2
Answers
You could try registering the event listener with a third param (T/F) denoting whether the event is captured or not.
Additionally, could you provide an example of using selectedNodes prior to storage in sessionStorage?
There is this
storage event
, but there is catch. The event is being only dispatched, when other pages on the domain using storage change something. There is no event dispatched for change being made by the same page, that owns the storage.You can create your own "event dispatch", by implementing some function to
set
/get
items from thesessionStorage
, something like this: