Basic coding question: Global Functions?

Hey all, I have a basic question about using global functions in Velo / Wix.
I want to create some functions to work across all pages. I figured, the masterPage.js is perfect for that.

Does anyone have advice on how to write a function to be accessed globally?
Thank you!

Hey @vineshgayadin!

That’s right, if you want the code to work across all pages, then masterPage.js is the place to put it. This will affect all global elements, like header, footer etc.

If you’re looking to use a function across multiple pages, but not all of them, then using the public code files is likely the best way to go.

Check out this doc to see where the best place to put your code is - Where Do I Put My Code?

Hi depending on the function you can store it in a backend module, or a public js file. Later you can import the function into any part of your site for example:

For backend:
import {myFunction} from 'backend/some-module.jsw'.

For public Js:
import {myFunction} from 'public/some-public-module.js'

The main difference between using the backend or public models is that the backend one is more secure and you can handle sensible data with the Wix secrets manager. In the case of public modules, they can be accessed from the browser and show all their values. So it should never be used for sensitive data.

Captura de Pantalla 2023-08-11 a la(s) 09.14.04
Captura de Pantalla 2023-08-11 a la(s) 09.13.48

1 Like

Very helpful, thank you @noahlovell and @matiasbarriga !

1 Like