I have a single line of code on my users' sites which requests a script from my server:
<script src="myserver.com/getjs?user=1"></script>
and php serves up the js, inserting the user's id in the code:
(function(){
var userid = 1; //written out from $_GET['user']
...js code...
})();
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:
...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']
<?php if( $userOption1 ){ ?>
function _option1(userid){ //func required if option1 true
...func1 code...
}
_option1(userid); //ruin it in js
<?php } ?>
<?php if( $userOption2 ){ ?>
function _option2(userid){ //func required if option2 true
...func2 code...
}
_option2(userid); //run it in js
<?php } ?>
...and so on...
})();
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.
Thanks to anyone who can help!
You could use Require.JS (website). It loads scripts asynchronously on demand. To load the library you can use:
<script data-main="js/loadScripts.php" src="libs/require.js"></script>
It basically tells Require.JS to run the "loadScripts.php" javascript file when finished loading (yes, loadScripts.php needs to return Javascript, there you can return custom libraries for any user).
Inside loadScripts.php, you could do something like:
<?php
// Generate en array of loading scripts, you'll probably want to dynamically
// generate this array according to user access permissions or any other case
$scriptsToLoad = array("script1", "script2", "script3");
$loadingList = "[";
$argumentsList = "";
foreach ($scriptsToLoad AS $script)
{
$loadingList .= "\"{$script}\", ";
$argumentsList .= "{$script}, ";
}
$loadingList = substr($loadingList, 0, strlen($loadingList) - 2);
$loadingList .= "]";
$argumentsList = substr($argumentsList, 0, strlen($argumentsList - 2);
echo "require({$loadingList}, function({$argumentsList}) {});
";
?>
I haven't tested this, but you can get an idea reading at its website. Note that scripts are called without ".js" and need to be in the same folder of loadScripts (they can be inside subfolders, calling like subfoldername/scriptName
to load the file js/subfoldername/scriptName.js
).
The argumentsList will define variables which will contain the modules loaded. The modules will be defined like:
$(function() {
// Stuff...
var module = {
doSomething: function() {
// .......
},
anyValueYouWant: 5
};
return module;
});
Of course, there are LOTS of ways to define modules, you'll need to read the API docs. Hope this helps.