Global variable accessible from all pages

Hi

On each page of my website I need to load the same data from a database. My website is composed of a header page which is common to all pages.
Is there a possibility to load the database only once and access the retrieved value from any page of the site?

Cheers, Mickael

I definately need this too.

Count me too

You can use session storage API to store data between pages.

On the first page, load the data from the database, then store it in storage.

On the second page, check if your have saved data. If so, use it. If not, load it from the database.

Checkout the session storage in the reference. It acts like a global variable on the site.

It’s not possible to have a global constant string or so?
If i have a const in a page (const string test = “test”) then i is the same amount of code (or even more) to call for that const.
Like - if i store the string test in the session data - then i still have to write session.get(“test”) to get that test string. There’s no code saved and in the end it’s actually more code because i have to declare a variable and also get the data via a identifier…

We need something like a global variable - like you define it in the site as global const string test = “jfjfjaofijaofj”; and then use it on all pages with just calling the test variable…

In that case, Cloudy, you may consider to create a public js file (let’s name it vars.js) with the definition of a globalVars object containing all the constants you need in your site, by using an expression like this:

export let globalVars = {
test : “jfjfjaofijaofj”,
test2 : “what does ‘jfjfjaofijaofj’ mean BTW”
};

Then in each webpage you can simply import that object by using this line:
import {globalVars} from ‘public/vars.js’;
// → And directly access the object internal properties like this:
Console.log(globalVars.test); //which should output: jfjfjaofijaofj

That’s the best answer i’ve found on this forum so far!! Thank you very much!!!

It looks nice !!! I will try !!!

Hi, I like your solution.
In the Export let globalVars you define varfiables that are statics. How can I define a variable that changes according to some criterias stored on the Collection for a specific logged in user?

Thank you
Pierre Lacasse

But if I changed the value of globalVars.test , It’s not changed in another page.

When I put ‘import {globalVars} from ‘public/vars.js’;’ I am getting an error stating public/vars.js is not recognised. What can I do?

@cliffhegan This thread is over two years old, so please create a new post with all the necessary details so you can get support. Refer to our guidelines to see what you should include in your post. Thanks.