I have a JavaScript file which has a hard coded BASEURL variable, this value is then used by other functions in the file. I would like for this url value to be set dynamically so that I don't need to manually change it for different installs. Is it possible to insert a PHP variable value into a JavaScript file?
|
Rather than try to mix PHP into javascript files, here's what I do in the HTML template:
This sets the variable and allows all your embedded scripts to access it. |
|||||
|
Suppose you have a JS file written on the fly by PHP; file1.php
The client-side source code of file1.php?var1=10 will then be
|
|||
|
There exists several ways:
|
|||||||||
|
You will need to make your file a php file, not a js file. From there you can include any PHP tags in the file to work your dynamic magic. The key to make this whole thing work is in the headers, which you will need to set like so:
This technique can be used to for CSS files as well. |
|||||||||
|