How to get the query string by javascript?

前端 未结 10 1457
深忆病人
深忆病人 2020-11-27 03:36

How to extract the query string from the URL in javascript?

Thank you!

相关标签:
10条回答
  • 2020-11-27 04:10

    Here's the method I use...

    function Querystring() {
        var q = window.location.search.substr(1), qs = {};
        if (q.length) {
            var keys = q.split("&"), k, kv, key, val, v;
            for (k = keys.length; k--; ) {
                kv = keys[k].split("=");
                key = kv[0];
                val = decodeURIComponent(kv[1]);
                if (qs[key] === undefined) {
                    qs[key] = val;
                } else {
                    v = qs[key];
                    if (v.constructor != Array) {
                        qs[key] = [];
                        qs[key].push(v);
                    }
                    qs[key].push(val);
                }
            }
        }
        return qs;
    }
    

    It returns an object of strings and arrays and seems to work quite well. (Strings for single keys, arrays for the same key with multiple values.)

    0 讨论(0)
  • 2020-11-27 04:12
    // Assuming "?post=1234&action=edit"
    
    var urlParams = new URLSearchParams(window.location.search);
    
    console.log(urlParams.has('post')); // true
    
    console.log(urlParams.get('action')); // "edit"
    
    console.log(urlParams.getAll('action')); // ["edit"]
    
    console.log(urlParams.toString()); // "?post=1234&action=edit"
    
    console.log(urlParams.append('active', '1')); // "?post=1234&action=edit&active=1"
    
    0 讨论(0)
  • 2020-11-27 04:13

    If you're referring to the URL in the address bar, then

    window.location.search
    

    will give you just the query string part. Note that this includes the question mark at the beginning.

    If you're referring to any random URL stored in (e.g.) a string, you can get at the query string by taking a substring beginning at the index of the first question mark by doing something like:

    url.substring(url.indexOf("?"))
    

    That assumes that any question marks in the fragment part of the URL have been properly encoded. If there's a target at the end (i.e., a # followed by the id of a DOM element) it'll include that too.

    0 讨论(0)
  • 2020-11-27 04:15

    There is a new API called URLSearchParams in browsers which allow you to extract and change the values of the query string.

    Currently, it seems to be supported in Firefox 44+, Chrome 49+ and Opera 36+.

    Initialize/Input

    To get started, create a new URLSearchParams object. For current implementations, you need to remove the "?" at the beginning of the query string, using slice(1) on the querystring, as Jake Archibald suggests:

    var params = new URLSearchParams(window.location.search.slice(1)); // myParam=12
    

    In later implementations, you should be able to use it without slice:

    var params = new URLSearchParams(window.location.search); // myParam=12
    

    Get

    You can get params from it via the .get method:

    params.get('myParam'); // 12
    

    Set

    Params can be changed using .set:

    params.set('myParam', 'newValue');
    

    Output

    And if the current querystring is needed again, the .toString method provides it:

    params.toString(); // myParam=newValue
    

    There are a host of other methods in this API.

    Polyfill

    As browser support is still pretty thin, there is a small polyfill by Andrea Giammarchi (<3kB).

    0 讨论(0)
提交回复
热议问题