1

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?

4

3 回答 3

0

你可以做的是有一个 PHP 文件来写入这些 JavaScript 变量,方式如下:

$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>\n");

然后,您就可以在 JavaScript中使用doThat和变量了。playThat

希望这可以帮助。

于 2013-02-02T11:17:22.067 回答
0

Depends on who needs to have access to those variables.

  1. If these variables are shared between all your visitors you should definitly save them on the server.
  2. If you need them only per user and per session, save them via PHP in a session or with cookies at the user. Cookies can be created with JS aswell
  3. If you need them only per user but persitant you can use the localStorage-Object. Nearly all modern browsers support this.
  4. If you have a login-script and you need the variables persistent and per user you should use php, since this is the most reliable way to identify a user over time.
于 2013-02-02T10:33:30.007 回答
0

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.

于 2013-02-02T10:34:47.003 回答