global variables in framework

i would like to define a global variable to use across pages in a framework application.
how is that done.

so far, it appears that variables are readable within a .md document and writable within a javascript block, but their scope is limited to just one .md document.

2 Likes

This is on the roadmap, but not implemented yet. See Custom implicit imports ¡ Issue #928 ¡ observablehq/framework ¡ GitHub

i have tried to use localStorage - which is described to span pages, but find that switching panes clears or replaces it.
is that to be expected?

Sorry, my answer was off-topic, I thought you wanted to share a variable definition across pages.

Yes, both localStorage and sessionStorage make it possible to store a variable value, with some differences. See Stored inputs | Pangea Proxima for an example of each.

i thought it would work, but find that a value which was set in one pane is not always present in another pane. some of it has to do with the order in which they are viewed, but even switching between panes which only read the value can lead to a state in which a value which was set in some initial pane is no longer present in the localStorage.

Ah, you want two separate pages to share a reactive state? I don’t know if that’s possible with localStorage. I had done something like this a while ago and it involved a websocket and a heroku server — even allowing the synchronization of several unrelated clients.

Mike has an example with the Broadcast Channel API, which might be closer to what you’re after. Broadcast Channel / Mike Bostock | Observable
This API is limited to one browser (can’t share between computers).

it is certainly possible, but the mechanism is not clear.
for example, a value set on the initial page of a framework application can be read on the first page reached from the navigation bar.
the same was true going from that second page to the third.
somehow then, going back to the second page loses the entry.