How to get URL parameter using jQuery or plain JavaScript?

前端 未结 30 3209
天涯浪人
天涯浪人 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 07:00

    This will give you a nice object to work with

        function queryParameters () {
            var result = {};
    
            var params = window.location.search.split(/\?|\&/);
    
            params.forEach( function(it) {
                if (it) {
                    var param = it.split("=");
                    result[param[0]] = param[1];
                }
            });
    
            return result;
        }
    

    And then;

        if (queryParameters().sent === 'yes') { .....
    
    0 讨论(0)
  • 2020-11-21 07:01

    So simple you can use any url and get value

    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }
    

    Usage Example

    // query string: ?first=value1&second=&value2
    var foo = getParameterByName('first'); // "value1"
    var bar = getParameterByName('second'); // "value2" 
    

    Note: If a parameter is present several times (?first=value1&second=value2), you will get the first value (value1) and second value as (value2).

    0 讨论(0)
  • 2020-11-21 07:01

    If you want to find a specific parameter from a specific url:

    function findParam(url, param){
      var check = "" + param;
      if(url.search(check )>=0){
          return url.substring(url.search(check )).split('&')[0].split('=')[1];
      }
    }  
    
    var url = "http://www.yourdomain.com/example?id=1&order_no=114&invoice_no=254";  
    alert(findParam(url,"order_no"));
    
    0 讨论(0)
  • 2020-11-21 07:01

    use this

    $.urlParam = function(name) {
      var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
      return results[1] || 0;
    }
    
    0 讨论(0)
  • 2020-11-21 07:02

    Solution from 2020

    We have: http://example.com?sent=yes

    let searchParams = new URLSearchParams(window.location.search)
    

    Does sent exist?

    searchParams.has('sent') // true
    

    Is it equal to "yes"?

    let param = searchParams.get('sent')
    

    and then just compare it.

    0 讨论(0)
  • 2020-11-21 07:02

    Using URLSearchParams:

    var params = new window.URLSearchParams(window.location.search);
    console.log(params.get('name'));
    

    Be careful about the compatibility (Mostly it's fine, but IE and Edge, may be different story, check this for compatible reference: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)

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