Reading environment variables with Javascript
FWIW.... Just playing around with an .HTA file (yes that uses the MSIE 'engine' so you can probably forget about other browsers) and the following JavaScript reads the %TEMP%
path variable to set a filepath+name for safely writing a temporary file. This works OK for me :
var oTest = new ActiveXObject("wscript.shell");
pathTest = oTest.ExpandEnvironmentStrings("%TEMP%") + "\\test_it.txt";
oTest = null;
alert(pathTest); // proves we can read %TEMP%
// and create the file, to complete the example
var oFSO = new ActiveXObject("Scripting.FileSystemObject");
var fil = oFSO.CreateTextFile(pathTest, true);
fil.WriteLine("Hello wonderful world!");
fil.Close();
All of the answers are today (at the time of writing) long outdated. :)
Of course, JS is still sandboxed, but that does not prevent anyone from using something like environment variables in client software. What you need to know and use wisely is the transfer of data of any kind from the operating system into a front-end application.
Several front-end frameworks may help here. E.g. an Angular application created with @angular/cli provides use cases for different environments out of the box. In an application configuration file (e.g. angular.json) you can configure the options, variables, values, whatever you need that you transport into your front end application via e.g. webpack. They may only be read once and can not change at runtime.
Following library nwjs.io tries something new: it lets you call all Node.js modules directly from DOM.
In docked/docker environments, there are other expectations for backend and frontend software, and these environments are configured differently and properly constrained from the OS's point of view.
the javascript Date() Object provides the clients system time.
szpic
, if Javascript were not sandboxed from the OS, any half-witted criminal could get onto your computer and take control. Allowing environmental variables is way too low level to ever be allowed.
Take a look at all of your environmental variables and ask yourself if everyone on the planet should have access to all that data about your computer.
If you want to do this with Javascript, you'll have to use a special web browser, such as node-webkit. It isn't going to run on any normal web browser, though, so don't expect more than 0.0001% of the population to expect to run your page.
I don't know your intended use, but one trick, to throw in an environmental variable, is to make a shortcut to your web application, and include it in there as a URL parameter (at least on Winblows).
http://myapp.com?computername=%COMPUTERNAME%