-iom-

Javascript and queries

posted: 22 Apr 2012

Here’s a little javascript snippet that I wrote that extracts the query string from the url and stores it in an object (key value pairs).

 1 // javascript
 2 
 3 (function() {
 4 	var extractQ = function() {
 5 		var qo = {}, 
 6 				 q = (arguments.length > 0) ? arguments[0] : document.location.search;
 7 		if (q) {
 8 			q = q.replace('?', '').split('&');
 9 			for (var i = 0; i < q.length; i++) {
10 				if (q[i] != "") {
11 					kv = q[i].split('=');
12 					qo[kv[0]] = (kv[1] == undefined) ? "" : kv[1];
13 				}
14 			}
15 		}
16 		return qo;
17 	};
18 })();

Check out this script at work!