Thursday, April 23, 2009

Finding Url Parameters using Javascript

Sometimes you want to get the parameters of a url ? In jsp, you can use something like
[source lang="jscript"]

var name='<%=request.getParameter("name") %>';

if(name==='null'){
name='';
}
[/source]
Now I found a much better way using Javascript at this web site . The code is below for reference.

[source lang="jscript"]
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
[/source]

The website also has other articles with regards to web programming and javascript.