How to get the query string by javascript?

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

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

Thank you!

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

    You can use this Javascript :

    function getParameterByName(name) {
        var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    }
    

    OR

    You can also use the plugin jQuery-URL-Parser allows to retrieve all parts of URL, including anchor, host, etc.

    Usage is very simple and cool:

    $.url().param("itemID")
    

    via James&Alfa

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

    Very Straightforward!

    function parseQueryString(){
        var assoc = {};
        var keyValues = location.search.slice(1).split('&');
        var decode = function(s){
            return decodeURIComponent(s.replace(/\+/g, ' '));
        };
    
        for (var i = 0; i < keyValues.length; ++i) {
            var key = keyValues[i].split('=');
            if (1 < key.length) {
                assoc[decode(key[0])] = decode(key[1]);
            }
        }
    
        return assoc;
    }
    
    0 讨论(0)
  • 2020-11-27 04:03

    You need to simple use following function.

    function GetQueryStringByParameter(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
            return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
    

    --- How to Use ---

    var QueryString= GetQueryStringByParameter('QueryString');
    
    0 讨论(0)
  • 2020-11-27 04:03

    Works for me-

    function querySt(Key) {
    
        var url = window.location.href;
    
        KeysValues = url.split(/[\?&]+/); 
    
        for (i = 0; i < KeysValues.length; i++) {
    
                KeyValue= KeysValues[i].split("=");
    
                if (KeyValue[0] == Key) {
    
                    return KeyValue[1];
    
            }
    
        }
    
    }
    
    function GetQString(Key) {     
    
        if (querySt(Key)) {
    
             var value = querySt(Key);
    
             return value;        
    
        }
    
     }
    
    0 讨论(0)
  • 2020-11-27 04:03

    I have use this method

    function getString()
    {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
    }
    var buisnessArea = getString();
    
    0 讨论(0)
  • 2020-11-27 04:06

    You can easily build a dictionary style collection...

    function getQueryStrings() { 
      var assoc  = {};
      var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
      var queryString = location.search.substring(1); 
      var keyValues = queryString.split('&'); 
    
      for(var i in keyValues) { 
        var key = keyValues[i].split('=');
        if (key.length > 1) {
          assoc[decode(key[0])] = decode(key[1]);
        }
      } 
    
      return assoc; 
    } 
    

    And use it like this...

    var qs = getQueryStrings();
    var myParam = qs["myParam"]; 
    
    0 讨论(0)
提交回复
热议问题