Skip to content

Instantly share code, notes, and snippets.

@pappelt
Created January 14, 2013 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pappelt/4531044 to your computer and use it in GitHub Desktop.
Save pappelt/4531044 to your computer and use it in GitHub Desktop.
Hand over params into js-files via URL
Put this into the file, where you want to use the params:
function parseQuery ( query ) {
var Params = new Object ();
if ( ! query ) return Params; // return empty object
var Pairs = query.split(/[;&]/);
for ( var i = 0; i < Pairs.length; i++ ) {
var KeyVal = Pairs[i].split('=');
if ( ! KeyVal || KeyVal.length != 2 ) continue;
var key = unescape( KeyVal[0] );
var val = unescape( KeyVal[1] );
val = val.replace(/\+/g, ' ');
Params[key] = val;
}
return Params;
}
var scripts = document.getElementsByTagName('script');
var myScript = scripts[ scripts.length - 1 ];
var queryString = myScript.src.replace(/^[^\?]+\??/,'');
var params = parseQuery( queryString );
Hand over the params like this:
<script src="{#cdnURL#}/js/main_admin.js?wwwURL={#wwwURL#}&cdnURL={#cdnURL#}"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment