# global variables in framework

**URL:** <https://talk.observablehq.com/t/global-variables-in-framework/9534>\
**Category:** Help\
**Created:** [July 22, 2024, 4:26am UTC](https://talk.observablehq.com/t/global-variables-in-framework/9534 "2024-07-22T04:26:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lomoramic](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/lomoramic/32/3503_2.png) [@lomoramic](https://talk.observablehq.com/u/lomoramic)\
**Post date:** [July 22, 2024, 4:26am UTC](https://talk.observablehq.com/t/global-variables-in-framework/9534/1 "2024-07-22T04:26:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [July 22, 2024, 8:14am UTC](https://talk.observablehq.com/t/global-variables-in-framework/9534/2 "2024-07-22T08:14:17Z")

</div>

This is on the roadmap, but not implemented yet. See [Custom implicit imports · Issue #928 · observablehq/framework · GitHub](https://github.com/observablehq/framework/issues/928)

---

<div class="post-metadata">

**Author:** ![lomoramic](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/lomoramic/32/3503_2.png) [@lomoramic](https://talk.observablehq.com/u/lomoramic)\
**Post date:** [July 23, 2024, 8:35am UTC](https://talk.observablehq.com/t/global-variables-in-framework/9534/3 "2024-07-23T08:35:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [July 23, 2024, 9:15am UTC](https://talk.observablehq.com/t/global-variables-in-framework/9534/4 "2024-07-23T09:15:16Z")

</div>

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](https://observablehq.observablehq.cloud/pangea/support/stored-inputs) for an example of each.

---

<div class="post-metadata">

**Author:** ![lomoramic](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/lomoramic/32/3503_2.png) [@lomoramic](https://talk.observablehq.com/u/lomoramic)\
**Post date:** [July 23, 2024, 9:41am UTC](https://talk.observablehq.com/t/global-variables-in-framework/9534/5 "2024-07-23T09:41:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [July 23, 2024, 5:50pm UTC](https://talk.observablehq.com/t/global-variables-in-framework/9534/6 "2024-07-23T17:50:01Z")

</div>

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](https://observablehq.com/@mbostock/broadcast-channel)  
This API is limited to one browser (can’t share between computers).

---

<div class="post-metadata">

**Author:** ![lomoramic](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/lomoramic/32/3503_2.png) [@lomoramic](https://talk.observablehq.com/u/lomoramic)\
**Post date:** [July 23, 2024, 6:42pm UTC](https://talk.observablehq.com/t/global-variables-in-framework/9534/7 "2024-07-23T18:42:37Z")

</div>

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.
