Created
December 31, 2016 18:47
-
-
Save glmdev/a49cf1a38c192b10650b6ec80f1f27e5 to your computer and use it in GitHub Desktop.
Simple script to recreate PHP's GET variables in JavaScript.
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
/* Coded by Garrett Mills | |
* http://glmdev.github.io/ | |
* Last Edited on 31 December 2016 12:45 | |
* This code is licensed under the GNU GPL v3.0 | |
*/ | |
//To use, simply execute the script as the first item in the head tag | |
var ctr1 = 0 | |
var ctr2 = 0 | |
var ctr3 = 0 | |
var varlist | |
var varnames = []; | |
var varvals = []; | |
var _get = {}; | |
function isNumeric(n) { | |
return !isNaN(parseFloat(n)) && isFinite(n); | |
} | |
function isBoolean(str) { | |
return (str === 'true' || str === 'false'); | |
} | |
var varlist = window.location.search; //get variables extention from url | |
var varlist = varlist.replace("?", ""); //remove the '?' | |
var varlist = varlist + "&"; //add an extra '&' for formatting | |
var varlist = varlist.split("&"); //create an array for everything split by '&' | |
for (ctr1 = 0; ctr1 < varlist.length; ++ctr1) { //get the variable name only by removeing everything after the '=' | |
var currVarName = varlist[ctr1]; | |
currVarName = currVarName.substring(0, currVarName.indexOf('=')); //remove value and '=' | |
varnames.push(currVarName); //create an array of the names | |
} | |
for (ctr2 = 0; ctr2 < varlist.length; ++ctr2) { //get the variable valus only by removing everything before the '=' | |
var currVarVal = varlist[ctr2]; | |
currVarVal = currVarVal.substring(currVarVal.indexOf("=") + 1); | |
varvals.push(currVarVal); //write to values array | |
} | |
for (ctr3 = 0; ctr3 < varlist.length -1; ++ctr3) { | |
_get[varnames[ctr3]] = varvals[ctr3]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For urls in the following format:
http://website.address/page.html?key=pair&key2=pair2
Access them in JS using the
_get
array:alert(_get[key])