Web Tutorials     [RO]  [EN]  
| HOME | Tutorials | News | SERVICES | Directory | Tools | FORUM | About | SITE MAP | CONTACT | SEARCH |
.....................................................
.....................................................
User happy birthdayToday we celebrate one day of birth.
(dannyb0y)
.....................................................
Login
Register
I forgot my password
.....................................................
HangMan Online Game
.....................................................
Online
In total there is
9 visitors online,
of which:
1 invited
8 are bots
.....................................................
Put your ad here
.....................................................
.....................................................
.....................................................
.....................................................
.
Home - Parsing a URL with variables with raw Javascript

<< What's up with web hosting?   -   working with files htaccess >>
Rate this article(Members only)
1 2 3 4 5
A - A Announced this way the site administrator for any problems observed on this page.  Print this page as PDF  Email  

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 licence

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.

Add a comment on this article (members only - login on the site):
Put your ad here