you don't need any server code to catch URL params on a script url, passing that data to the script within. that's actually pretty simple.
throw this in the external script file:
Code:
function parseQS(str) {
var ob = {}, float = "",
key = "",
dc = decodeURIComponent;
for (var i = 0, mx = str.length; i < mx; i++) {
var it = str[i];
if (it === "=") {
key = float;
float = "";
continue;
}
if (!it.search(/^[?&]/)) {
if (it === "&" && str.slice(i + 1, i + 5) === "amp;") {
i = (i + 4);
float += "&";
continue;
}
if (key) {
ob[key] = dc(float);
}
key = "";
float = "";
continue;
}
float += it;
}
ob[key] = dc(float);
return ob;
}
var scripts=document.getElementsByTagName("script");
var options=parseQS(scripts[scripts.length-1].src);
alert(
JSON.stringify( options , null , '\t' )
)
so long as you don't use a
defer or
async attrib on the externally-pointing script tag, this will work just fine.
if used on "/scripts/test.js?a=1&b=2&c=hello%20world", you should get this object:
Code:
{
"a": "1",
"b": "2",
"c": "hello world"
}
this can allow the people using the embed to adjust the url params to set options like color, ID, etc. you could use a URL-builder form interface to allow such config options to be set using a GUI instead of editing params, much like vimeo or youtube does. this is all you need if your external script can handle all the options.