Created
April 8, 2015 16:03
-
-
Save tlivings/c674c6b1c08d78b88fd7 to your computer and use it in GitHub Desktop.
Passing parameters to javascript files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var scripts = document.getElementsByTagName('script'); | |
var myScript = scripts[ scripts.length - 1 ]; | |
var queryString = myScript.src.replace(/^[^\?]+\??/,''); | |
var params = parseQuery( queryString ); | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment