Nearly all server-side programming languages have built-in functions to retrieve querystring values of an URL. In web browsers you can access the querystring with client-side JavaScript, but there is no standard way to parse out the name/value pairs. So here is a function to return a parameter you specify. The following javascript code snippet facilitates Javascript's built in regular expressions to retrieve value of the key. Optionally, you can specify a default value to return when key does not exist.
<script language="javascript" type="text/javascript">
function getParamValue(key, default_)
{
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}
function GetUrl()
{
var a=getParamValue('a',' ');
var b=getParamValue('b',' ');
if(a!=' ' && b!=' ')
alert("a: "+a+"; b: "+b);
}
</script>
</head>
<body onload="GetUrl();">
<body>
Tag: javascript, javascript function, query string, querysting in javascript, url parameters
<script language="javascript" type="text/javascript">
function getParamValue(key, default_)
{
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}
function GetUrl()
{
var a=getParamValue('a',' ');
var b=getParamValue('b',' ');
if(a!=' ' && b!=' ')
alert("a: "+a+"; b: "+b);
}
</script>
</head>
<body onload="GetUrl();">
<body>
Tag: javascript, javascript function, query string, querysting in javascript, url parameters
No comments:
Post a Comment