Friday 7 March 2008

Stripping the URL parms using Javascript (quick tip)

If you need to look at the parm strings in the current URL here is a way to do it that loads
the parms as JS vars, so

http://myserver.com/mypage.html?openpage&q1=Steve&q2=McDonagh

Once the code below has run will result in 2 JS vars called q1 and q2 which you can then act on.

myArray = document.location.href.split('?')[1].split('&');
for(x=1; x<myArray.length; x++)
{
var tmp = myArray[x].split('=');
eval(unescape(tmp[0]) + '="' + unescape(tmp[1]) + '"');
}


The count x starts a 1 to avoid you having an undefined JS var called openpage

Slaun
Steve

No comments:

Disqus for Domi-No-Yes-Maybe