How to get query string values / URL Parameters in JavaScript? How to get query string values in JavaScript (URL Parameters)?

URLs parameters are the easiest way to pass the variables from one web page to the other web page. In this article, I will guide you to get the URL parameter in a simple manner.

Function to get query string values in JavaScript

<script type="text/javascript">
 
function getQueryStringVariable(variable)
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for(var i=0;i<vars.length;i++) 
  {
    var pair = vars[i].split("=");
    if(pair[0] == variable){return pair[1];}
  }
  return(false);
}

</script>

Output:

//Input: https://www.tech24qaguide.com/testpage.php?key1=value1&key2=value2&gowri=shankar

console.log(getQueryStringVariable('key1'));   // Output: value1

console.log(getQueryStringVariable('key2'));   // Output: value2

console.log(getQueryStringVariable('gowri'));   // Output: shankar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign In

Register

Reset Password

Please enter your username or email address, you will receive a link to create a new password via email.