glmdev
12/31/2016 - 6:47 PM

Simple script to recreate PHP's GET variables in JavaScript.

Simple script to recreate PHP's GET variables in JavaScript.

/* 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];
}