Javascript – Using the GET parameter of a URL in JavaScript


If I am on a page such as

In the JavaScript of that page, I would like to set a variable to the value of the parameter in the GET part of the URL.

So in JavaScript:

   param1var = ...   // ... would be replaced with the code to get asdf from URI

What would "…" be?

Best Solution

Here's some sample code for that.

var param1var = getQueryVariable("param1");

function getQueryVariable(variable) {
  var query =;
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
  alert('Query Variable ' + variable + ' not found');