Saturday, March 27, 2010

Get URL Parameters Using Javascript Sample Code

function getUrlParameter( 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];
}


if the URL is http://www.foo.com/index.html?bob=123&frank=321&tom=213#top


then you can read parameter value of frank by


var valueOfFrank = getUrlParameter('frank');




Original article from : http://www.netlobo.com/url_query_string_javascript.html

No comments:

Post a Comment