Parsing a URL with variables with raw Javascript
Publishing date: 28-01-2011 -
Copyright © Claudiu Gilcescu-Ceia
Sometimes, when working with AJAX content, we might like to pass a reply in the form of an URL with variables. In order to do so, we need to have a way of parsing that URL and obtaining an associative Javascript array that we can use. Here's a simple, platform/framework/library independent code snippet that will provide what we need:
CODE:
var data = 'var1=This+is+a+string&var2=false&var3=300';
var replies = data.split('&');
var vars = [];
for(var i = 0; i < replies.length; i++) {
var reply = replies[i].split('=');
vars.push(reply[0]);
vars[reply[0]] = reply[1];
}
alert (vars['var2']); // Outputs "false" Of course, ideally, you would use that as a function that takes the URL string as a parameter and returns the associative array. The code above is just for demo purposes.
The code in this article is distributed under the MIT license ![]()
Publishing date: 28-01-2011 -
Copyright © Claudiu Gilcescu-Ceia
Click here if you want to see other articles by the same author
| There are no comments on this article. Be the first to say your opinion. |






