How to obtain get parameters from URL with javascript
by Arxleol on Tuesday 09.06.2009, under javascript, tutorial
In previous post I showd small script that enabled players of certain online game to faster link to specialized URL. Script used get parameters from the URL to form new link to the web page.
Therefore I decieded to cut away part of code that obtained get parameters out and present it separetly. So here it is:
var gp=new Array(); var loc=location.search; if (loc){ loc=loc.substring(1); var parms=loc.split('&'); for(var i=0;i<parms.length;i++){ nameValue=parms[i].split('='); gp[nameValue[0]]=unescape(nameValue[1]); } } gp['village'];
There is nothing special here. Code splits URL value and then finds each separate variable and its respective value and store them in the array. You will be able to obtain value of variable by referring to it by name, in the gp array.
If you have any problems feel free to ask!