In order to use PHP code in any file, the web server has to run that file through the PHP processor. This is configured to happen by default with .php
files, but not with .js
files.
You could configure your server to process .js
files as PHP, but that's generally unwise. At the very least, it creates a lot of unnecessary overhead for those files since most of them won't (or shouldn't) have PHP code.
Without knowing more about the structure of what you're trying to accomplish, it's difficult to advise a "best" approach. Options include (but may not be limited to):
- Defining that one
var
on a PHP page which references the JS file, thereby making it available to the JavaScript code.
- Putting the value in a page element somewhere that it can be accessed by JavaScript code, either as a form value or perhaps a data value.
- Making an AJAX request to the server to get that value (and other values) after the page has been loaded.