How to get URL parameter using jQuery or plain JavaScript?

前端 未结 30 3189
天涯浪人
天涯浪人 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:52

    Perhaps you might want to give Dentist JS a look? (disclaimer: I wrote the code)

    code:

    document.URL == "http://helloworld.com/quotes?id=1337&author=kelvin&message=hello"
    var currentURL = document.URL;
    var params = currentURL.extract();
    console.log(params.id); // 1337
    console.log(params.author) // "kelvin"
    console.log(params.message) // "hello"
    

    with Dentist JS, you can basically call the extract() function on all strings (e.g., document.URL.extract() ) and you get back a HashMap of all parameters found. It's also customizable to deal with delimiters and all.

    Minified version < 1kb

    0 讨论(0)
  • 2020-11-21 06:53
    http://example.com?sent=yes
    

    Best solution here.

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

    With the function above, you can get individual parameter values:

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

    Try this working demo http://jsfiddle.net/xy7cX/

    API:

    • inArray : http://api.jquery.com/jQuery.inArray/

    This should help :)

    code

    var url = "http://myurl.com?sent=yes"
    
    var pieces = url.split("?");
    alert(pieces[1] + " ===== " + $.inArray("sent=yes", pieces));
    
    0 讨论(0)
  • 2020-11-21 06:55

    There's this great library: https://github.com/allmarkedup/purl

    which allows you to do simply

    url = 'http://example.com?sent=yes';
    sent = $.url(url).param('sent');
    if (typeof sent != 'undefined') { // sent exists
       if (sent == 'yes') { // sent is equal to yes
         // ...
       }
    }
    

    The example is assuming you're using jQuery. You could also use it just as plain javascript, the syntax would then be a little different.

    0 讨论(0)
  • 2020-11-21 06:55

    This is based on Gazoris's answer, but URL decodes the parameters so they can be used when they contain data other than numbers and letters:

    function urlParam(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        // Need to decode the URL parameters, including putting in a fix for the plus sign
        // https://stackoverflow.com/a/24417399
        return results ? decodeURIComponent(results[1].replace(/\+/g, '%20')) : null;
    }
    
    0 讨论(0)
提交回复
热议问题