JSFiddle - React, Tailwind, and code Playground
by Toukakoukan
JavaScript
function buildValue(sValue) {
if (/^\s*$/.test(sValue)) { return(null); }
if (/^(true|false)$/i.test(sValue)) { return(sValue.toLowerCase() === "true"); }
if (isFinite(sValue)) { return(parseFloat(sValue)); }
if (isFinite(Date.parse(sValue))) { return(new Date(sValue)); }
return(sValue);
}
function getVars(url) {
var oGetVars = {};
var a = document.createElement('a');
a.href = url;
var iCouple, aCouples = a.search.substr(1).split("&");
for (var iCouplId = 0; iCouplId < aCouples.length; iCouplId++) {
iCouple = aCouples[iCouplId].split("=");
oGetVars[unescape(iCouple[0])] = iCouple.length > 1 ? buildValue(unescape(iCouple[1])) : null;
}
return oGetVars;
}
console.log(getVars('http://google.com?q=123&y=xyz'));