How to convert URL parameters to a JavaScript object? [duplicate]

In the year 2021… Please consider this obsolete. Edit This edit improves and explains the answer based on the comments. var search = location.search.substring(1); JSON.parse(‘{“‘ + decodeURI(search).replace(/”/g, ‘\\”‘).replace(/&/g, ‘”,”‘).replace(/=/g,'”:”‘) + ‘”}’) Example Parse abc=foo&def=%5Basf%5D&xyz=5 in five steps: decodeURI: abc=foo&def=[asf]&xyz=5 Escape quotes: same, as there are no quotes Replace &: abc=foo”,”def=[asf]”,”xyz=5 Replace =: abc”:”foo”,”def”:”[asf]”,”xyz”:”5 Suround with … Read more

Change URL parameters and specify defaults using JavaScript

I’ve extended Sujoy’s code to make up a function. /** * http://stackoverflow.com/a/10997390/11236 */ function updateURLParameter(url, param, paramVal){ var newAdditionalURL = “”; var tempArray = url.split(“?”); var baseURL = tempArray[0]; var additionalURL = tempArray[1]; var temp = “”; if (additionalURL) { tempArray = additionalURL.split(“&”); for (var i=0; i<tempArray.length; i++){ if(tempArray[i].split(‘=’)[0] != param){ newAdditionalURL += temp + … Read more

tech