Request.QueryString in JavaScript

Request.QueryString in JavaScript

Request.QueryString in JavaScript

Every now and then I find myself needing this function in JavaScript so I end up writing it over and over again. So I decided to post it on our blog and file it under Tip-And-Tricks so I (and, yeah, you too!) can come back and get it when needed again.

So here we go:


<script type="text/javascript">
<!--

function qs(key) {
 //Get the full querystring
 fullQs = window.location.search.substring(1);
 //Break it down into an array of name-value pairs
 qsParamsArray = fullQs.split("&");
 //Loop through each name-value pair and
 //return value in there is a match for the given key
 for (i=0;i<qsParamsArray.length;i++) {
  strKey = qsParamsArray[i].split("=");
  if (strKey[0] == key) {
   return strKey[1];
  }
 }
}

//Test the output (Add ProductId to your URL)
var ProductId = QueryString("ProductId");
document.write("Product Id: " + ProductId + " - Full Query String: " + fullQs);

-->
</script>

<h3>Usage:</h3>


var&nbsp;myValue&nbsp;=&nbsp;qs(&amp;quot;key&amp;quot;)

Enjoy.

mm
Antonio Chagoury

Hi, I'm Antonio, Founder and CEO of Maxiom Technology (formerly Inspector IT). I'm a technology executive and entrepreneur who has achieved consistent success in driving growth, generating revenue, and enhancing value in domestic and international markets through technology product innovations.

No Comments

Sorry, the comment form is closed at this time.