Wednesday, August 25, 2010

Read URL Parameters using JavaScript

Following is the javascript code for get the parameter "userId" from the url.
So the website url will be displayed as http:.....................whatever.html?userId=balh007&age=77
The following function will return "blah007"

function getUserId(){
name = 'userId';
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];
}

No comments: