Hi,
I’m working with static html and JavaScript with some secret variables in it. Now I want to use Environment variable feature to keep my variable’s value secret and I want to access those variables in JavaScript file. I did set environment variable on the setting section. But I can’t access them in my JavaScript. I don’t want to use any framework like node js or anything because I don’t have enough knowledge about those.
I’m a little newbie. So please help me.
Environment Variables are available only in the build phase of the website, not on the user’s browser when viewing the site.
This would make them not secret, of course.
The solution to keeping variables secret is to not use them in any public facing code, create a Pages’ Function and do the calculation there, with the environment variable you set-up (in the Functions section).
If you’re working with static HTML and JavaScript files, you won’t be able to access environment variables directly from your JavaScript code, as environment variables are typically only available on the server side.
However, there are a few ways you can work around this limitation:
- Use a build tool like Webpack or Gulp to replace placeholders in your HTML and JavaScript files with the actual environment variable values during the build process. For example, you could replace a placeholder like
{{API_KEY}}
in your code with the actual API key value from the environment variable. - Another option is to set the environment variable value in a script tag in your HTML file, and then access that value in your JavaScript code. For example:
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.