Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Modularising" Javascript file
    primarykey
    data
    text
    <p>I have a single line of code on my users' sites which requests a script from my server:</p> <pre><code>&lt;script src="myserver.com/getjs?user=1"&gt;&lt;/script&gt; </code></pre> <p>and php serves up the js, inserting the user's id in the code:</p> <pre><code>(function(){ var userid = 1; //written out from $_GET['user'] ...js code... })(); </code></pre> <p>Now I want to output different js depending what the user's id is. Rather than have lots of different js files, I want a way to "modularise" the code that's sent based on user settings I have in a db so users don't need to change the code on their site. The only way I can see of doing this is outputting the js file like this:</p> <pre><code>...first PHP will get some user settings from the db based on their userid... ...then the js is output... (function(){ var userid = 1; //written out from $_GET['user'] &lt;?php if( $userOption1 ){ ?&gt; function _option1(userid){ //func required if option1 true ...func1 code... } _option1(userid); //ruin it in js &lt;?php } ?&gt; &lt;?php if( $userOption2 ){ ?&gt; function _option2(userid){ //func required if option2 true ...func2 code... } _option2(userid); //run it in js &lt;?php } ?&gt; ...and so on... })(); </code></pre> <p>But I'm sure there must be a more efficient way to manage this. The reasons for doing this are partially to keep the code as small as possible for each user, but mainly because I am building features which some users will not want - it needs to be dynamic for each person.</p> <p>Thanks to anyone who can help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload