How to Convert GET URL Structure to Array and Reverse

[js]

function URLToArray(url) {
var request = {};
var pairs = url.substring(url.indexOf(‘?’) + 1).split(‘&’);
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split(‘=’);
request[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return request;
}

function ArrayToURL(array) {
var pairs = [];
for (var key in array)
if (array.hasOwnProperty(key))
pairs.push(encodeURIComponent(key) + ‘=’ + encodeURIComponent(array[key]));
return pairs.join(‘&’);
}
[/js]

Source: http://stackoverflow.com/questions/4297765/make-a-javascript-array-from-url


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *