I have created a JS script which has more optional settings.
So, somewhere near the beginning of the file I have something like this:
var doThat = true;
var playThat = false;
...
This script will be used by users and not by developers so they may not know how to edit a JavaScript file. How can I create a kind of admin-panel which would allow to change some variables in a JavaScript file.
I was thinking about creating an interface which will contain radio buttons to choose those values, now the problem is: how do I actually save those changes in the .js
file? Should I use PHP to edit the js file directly or is there some better way?
Depends on who needs to have access to those variables.
for this make an interface with radio button. then make all changes and save it to database.The saved result's corresponding work will be performed.
What you could do is have a PHP file that writes these JavaScript variables, in the following way:
$doThatVariable = true; // This variable could be retrieved from a database
$playThatVariable = false; // This variable could be retrieved from a database
// This code could be ran on relevant pages to correctly set up the JavaScript variables
print("<script type=\"text/javascript\">
var doThat = " . $doThatVariable . ";
var playThat = " . $playThatVariable . ";
</script>
");
You would then be able to use the doThat
and playThat
variables in your JavaScript.
Hope this helps.