How to get URL parameter using jQuery or plain JavaScript?

前端 未结 30 3241
天涯浪人
天涯浪人 2020-11-21 06:29

I have seen lots of jQuery examples where parameter size and name are unknown.

My URL is only going to ever have 1 string:

http://example.com?sent=ye         


        
相关标签:
30条回答
  • 2020-11-21 06:57

    This one is simple and worked for me

    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        return results[1] || 0;
    }
    

    so if your url is http://www.yoursite.com?city=4

    try this

    console.log($.urlParam('city'));
    
    0 讨论(0)
  • 2020-11-21 06:58

    jQuery code snippet to get the dynamic variables stored in the url as parameters and store them as JavaScript variables ready for use with your scripts:

    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        if (results==null) {
           return null;
        }
        return decodeURI(results[1]) || 0;
    }
    

    example.com?param1=name&param2=&id=6

    $.urlParam('param1'); // name
    $.urlParam('id');        // 6
    $.urlParam('param2');   // null
    

    example params with spaces

    http://www.jquery4u.com?city=Gold Coast
    console.log($.urlParam('city'));  
    //output: Gold%20Coast
    
    
    
    console.log(decodeURIComponent($.urlParam('city'))); 
    //output: Gold Coast
    
    0 讨论(0)
  • 2020-11-21 06:58

    Coffeescript version of Sameer's answer

    getUrlParameter = (sParam) ->
      sPageURL = window.location.search.substring(1)
      sURLVariables = sPageURL.split('&')
      i = 0
      while i < sURLVariables.length
        sParameterName = sURLVariables[i].split('=')
        if sParameterName[0] == sParam
          return sParameterName[1]
        i++
    
    0 讨论(0)
  • 2020-11-21 06:58

    What if there is & in URL parameter like filename="p&g.html"&uid=66

    In this case the 1st function will not work properly. So I modified the code

    function getUrlParameter(sParam) {
        var sURLVariables = window.location.search.substring(1).split('&'), sParameterName, i;
    
        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');
    
            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:59

    I always stick this as one line. Now params has the vars:

    params={};location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(s,k,v){params[k]=v})
    

    multi-lined:

    var params={};
    window.location.search
      .replace(/[?&]+([^=&]+)=([^&]*)/gi, function(str,key,value) {
        params[key] = value;
      }
    );
    

    as a function

    function getSearchParams(k){
     var p={};
     location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(s,k,v){p[k]=v})
     return k?p[k]:p;
    }
    

    which you could use as:

    getSearchParams()  //returns {key1:val1, key2:val2}
    

    or

    getSearchParams("key1")  //returns val1
    
    0 讨论(0)
  • 2020-11-21 06:59

    This might be overkill, but there is a pretty popular library now available for parsing URIs, called URI.js.

    Example

    var uri = "http://example.org/foo.html?technology=jquery&technology=css&blog=stackoverflow";
    var components = URI.parse(uri);
    var query = URI.parseQuery(components['query']);
    document.getElementById("result").innerHTML = "URI = " + uri;
    document.getElementById("result").innerHTML += "<br>technology = " + query['technology'];
    
    // If you look in your console, you will see that this library generates a JS array for multi-valued queries!
    console.log(query['technology']);
    console.log(query['blog']);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.17.0/URI.min.js"></script>
    
    <span id="result"></span>

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