javascript tutorial - [Solved-5 Solutions] How to get the value from the GET parameters - javascript - java script - javascript array



Problem:

From our code example, it looks like Array.prototype.find is what we are looking for: Array.prototype.find() and Array.prototype.findIndex()

[1, 2, 3].find(function(el) {
    return el === 2;
}); // returns 2

click below button to copy the code. By JavaScript tutorial team

Solution 1:

JavaScript itself has nothing built in for handling query string parameters. In a (modern) browser we can use the (experimental at time of writing) URL object ;

var url_string = "http://www.example.com/t.html?a=1&b=3&c=m2-m3-m4-m5";
var url = new URL(url_string);
var c = url.searchParams.get("c");
console.log(c);
click below button to copy the code. By JavaScript tutorial team

Run code snippet Expand snippet For older browsers, we can use the code from the original version of this answer that predates URL. We could access location.search, which would give we from the ? character on to the end of the URL or the start of the fragment identifier (#foo), whichever comes first. Then we can parse it with this:

function parse_query_string(query) {
  var vars = query.split("&");
  var query_string = {};
  for (var we = 0; we < vars.length; i++) {
    var pair = vars[i].split("=");
    // If first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = decodeURIComponent(pair[1]);
      // If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
      var arr = [query_string[pair[0]], decodeURIComponent(pair[1])];
      query_string[pair[0]] = arr;
      // If third or later entry with this name
    } else {
      query_string[pair[0]].push(decodeURIComponent(pair[1]));
    }
  }
  return query_string;
}

var query_string = "a=1&b=3&c=m2-m3-m4-m5";
var parsed_qs = parse_query_string(query_string);
console.log(parsed_qs.c);
click below button to copy the code. By JavaScript tutorial team

Run code snippet Expand snippetWe can get the query string from the URL of the current page with:

var query = window.location.search.substring(1);
var qs = parse_query_string(query);
click below button to copy the code. By JavaScript tutorial team

Solution 2:

JavaScript itself has nothing built in for handling query string parameters. In a (modern) browser we can use the (experimental at time of writing) URL object ;

var url_string = "http://www.example.com/t.html?a=1&b=3&c=m2-m3-m4-m5";
var url = new URL(url_string);
var c = url.searchParams.get("c");
console.log(c);
click below button to copy the code. By JavaScript tutorial team

Run code snippet Expand snippet For older browsers, we can use the code from the original version of this answer that predates URL. We could access location.search, which would give we from the ? character on to the end of the URL or the start of the fragment identifier (#foo), whichever comes first. Then we can parse it with this:

function parse_query_string(query) {
  var vars = query.split("&");
  var query_string = {};
  for (var we = 0; we < vars.length; i++) {
    var pair = vars[i].split("=");
    // If first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = decodeURIComponent(pair[1]);
      // If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
      var arr = [query_string[pair[0]], decodeURIComponent(pair[1])];
      query_string[pair[0]] = arr;
      // If third or later entry with this name
    } else {
      query_string[pair[0]].push(decodeURIComponent(pair[1]));
    }
  }
  return query_string;
}

var query_string = "a=1&b=3&c=m2-m3-m4-m5";
var parsed_qs = parse_query_string(query_string);
console.log(parsed_qs.c);
click below button to copy the code. By JavaScript tutorial team

Run code snippet Expand snippet We can get the query string from the URL of the current page with:

var query = window.location.search.substring(1);
var qs = parse_query_string(query);
click below button to copy the code. By JavaScript tutorial team

Solution 3:

	function gup( name, url ) {
    if (!url) url = location.href;
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( url );
    return results == null ? null : results[1];
}
gup('q', 'hxxp://example.com/?q=abc')

click below button to copy the code. By JavaScript tutorial team

Solution 4:

THIS IS AN EASY WAY TO CHECK JUST ONE PARAMETER: Example URL:

    http://myserver/action?myParam=2
click below button to copy the code. By JavaScript tutorial team

Example Javascript:

    var myParam = location.search.split('myParam=')[1]
click below button to copy the code. By JavaScript tutorial team

if "myParam" exists in the URL... variable myParam will contain "2", otherwise it will be undefined. Maybe we want a default value, in that case:

    var myParam = location.search.split('myParam=')[1] ? location.search.split('myParam=')[1] : 'myDefaultValue';
click below button to copy the code. By JavaScript tutorial team

(UPDATE) THIS WORKS BETTER:

    var url = "http://www.example.com/index.php?myParam=384&login=admin"; // or window.location.href for current url
    var captured = /myParam=([^&]+)/.exec(url)[1]; // Value is in [1] ('384' in our case)
    var result = captured ? captured : 'myDefaultValue';
click below button to copy the code. By JavaScript tutorial team

Solution 5:

We can get the query string in location.search, then we can split everything after the question mark:

var params = {};

if (location.search) {
    var parts = location.search.substring(1).split('&');

    for (var we = 0; we < parts.length; i++) {
        var nv = parts[i].split('=');
        if (!nv[0]) continue;
        params[nv[0]] = nv[1] || true;
    }
}

// Now we can get the parameters we want like so:
var abc = params.abc;

click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - How to get the value from the GET parameters