View Single Post
Old 01-30-2013, 04:34 AM   PM User | #7
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 01-30-2013 at 04:36 AM..
rnd me is offline   Reply With Quote