SoFunction
Updated on 2025-04-05

JavaScript implements 4 ways to get parameter values ​​in URLs

In front-end development, handling URL parameters is a common task, especially without framework support. This article has compiled 4 ways to get parameter values ​​in a URL in JavaScript, hoping it will be helpful to everyone.

Method 1: Regular method

function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = (1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
// Call this:alert(GetQueryString("Parameter name 1"));
alert(GetQueryString("Parameter name 2"));
alert(GetQueryString("Parameter name 3"));

Method 2: split split method

function GetRequest() {
    var url = ; //Get the string after the "?" character in the url    var theRequest = new Object();
    if (("?") != -1) {
        var str = (1);
        strs = ("&");
        for(var i = 0; i < ; i ++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
var Request = new Object();
Request = GetRequest();
// var parameter 1, parameter 2, parameter 3, parameter N;// Parameter 1 = Request['parameter 1'];// Parameter 2 = Request['parameter 2'];// Parameter 3 = Request['parameter 3'];// Parameter N = Request['parameter N'];

Method 3: Regular (recommended)

Getting url parameters through JS is often used. For example, a url: http://www./?q=js. If we want to get the value of the parameter q, we can use the following function to call it.

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = (1).match(reg); //Get the string after the "?" character in the url and match it regularly    var context = "";
    if (r != null)
        context = decodeURIComponent(r[2]);
    reg = null;
    r = null;
    return context == null || context == "" || context == "undefined" ? "" : context;
}
alert(GetQueryString("q"));

Method 4: Method for obtaining single parameters

function GetRequest() {
   var url = ; //Get the string after the "?" character in the url   if (("?") != -1) {    //Judge whether there are parameters      var str = (1); //Start from the first character because the 0th is the ? number Gets all strings separated from the question mark.      strs = ("=");   //Separate with equal sign (because I know there is only one parameter, so I directly use equal sign to separate if there are multiple parameters to be separated by & sign and then use equal sign to separate)      alert(strs[1]);          //The first parameter is popped up directly (if there are multiple parameters, the loop is still required)   }
}

This is the end of this article about 4 methods to obtain parameter values ​​in a URL by JavaScript. For more related content on JavaScript to obtain URL parameter values, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!