Get querystring value in jQuery

Many times we need client side requirement according to querystring value. Here i have create a function that return the value of querystring.

If your url is like this "www.yourwebsite.com/user.htm?fname=Nirbhay&lname=Singh", then use below code that retrieve the value of fname and lname.

CODE
<!DOCTYPE html>
<html>
<head>
  <title>jQuery With Example</title>
  <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  <script type="text/javascript">
      $(function () {
          $('.btnGetValue').click(function () {
              var fname = GetQueryStringValue('fname');
              var lname = GetQueryStringValue('lname');
              alert(fname + ' ' + lname);
          });

      });

      function GetQueryStringValue(param) {
          var pageURL = window.location.search.substring(1);
          var arrUrl = pageURL.split('&');
          for (var i = 0; i < arrUrl.length; i++) {
              var arrValue = arrUrl[i].split('=');
              if (arrValue[0] == param) {
                  return arrValue[1];
              }
          }
      }
  </script>
</head>
<body>
  <button class="btnGetValue">Get Value</button>
</body>
</html>

No comments :

Please Give Your Feedback