I’m trying to send a message between local javascript files. To test, I sent the message and added an event listener to the same file to process it. The file that sent it is receiving it, but the other local file is not.
Sending file:
confirmQuestion.addEventListener('click', function() {
window.postMessage('question set', '*')
}
window.addEventListener('message', function() {
otherOption3.value = 'asdf'
})
Receiving file:
window.addEventListener('message', function() {
localStorage.setItem('string', 'QUESTION SET')
})
window.addEventListener('load', function() {
option1Display.innerHTML = localStorage.getItem('string')
})
Might be related to this as described in MDN docs:
In all current browsers, localStorage
seems to return a different object for each file:
URL. In other words, each file:
URL seems to have its own unique local-storage area.
Is there a particular reason, why you use the local storage for a task like this rather than modules? If a task can be achieved with Javascript modules, it is better to use that rather than storing data locally on a computer which persists while the tab is already closed.
1 Like
Because I want to save data into the website. I’m not there yet. I just need a means of communication between files.
I know how to use local storage, I’m just trying to figure out why postMessage() isn’t working. The local storage is only there so the text display is there when I open the file.
Permanently? Or just as long the browser tab is opened and not reloaded?
Then I would recommend that you spend the time learning node modules. You’ll need it anyway. And using the local storage for a task that can be achieved with node modules would not be a good practice and therefore a waste of time, especially since it is apparently not working.
Probably because of the issue I posted above. Two local files aren’t referencing the same object of the local storage.
1 Like
ok thanks, I’ll spend some time learning node modules
1 Like