Hi @tonynsx, there seems to be some code missing – HTML will only show here if you format it as code, by selecting the code and clicking the </> button or directly using markdown syntax.
Anyway AFAIK you can only access elements on another page if you have a direct reference to that page, e.g. when you actively open()ed it or if it is embedded as an iframe:
const page = window.open('test.html')
page.addEventListener('DOMContentLoaded', () => {
// Now we can access the #test element on the other page
const testDiv = page.document.getElementById('test')
testDiv.textContent = 'Hello world!'
})
Well document refers to the current document; if you want to access elements inside another document, you first need a reference to that window as shown above.
Now the localStorage can indeed be used to communicate between two different windows via storage events; you won’t be able to get a reference to the other window this way, but you can transmit messages. For example:
Sorry, but I don’t understand. I have main.html, test.html and script.js in the same folder. So how come script.js know document.getElementById is referring to main.html?
Ah okay, well your JS is not a persistently running program shared between your pages, no matter where it is located; instead, it is getting executed anew for each page loading it, and only in the context of that page. So when loaded into main.html it will find the #test element but not #tt; and when loaded into test.html it’s the other way round.
It depends what exactly you want to do… using the local storage works and is simple, but also rather limited and not really intended for complex communication. A better way is using postMessage(), but this requires again a reference to the window you want to communicate with.
Yet another way is communication via your backend; I’m not familiar with eel, but from a quick look it seems to provide a way to directly expose a python API to your JS… so maybe you could use this as a channel to communicate between your 2 pages?
I was actually rather intrigued by eel, so I’ve been playing around a bit… FWIW you can indeed use it to communicate between your windows, here’s a simply PoC:
However, if at all possible I’d rather open() the other window from the main window so that you can send messages without using the python backend as an intermediary, which will certainly be much slower. Also, with a window reference you might not even need to send messages but can access and modify the other document directly.