Get escaped URL parameter

后端 未结 19 2676
小蘑菇
小蘑菇 2020-11-21 13:41

I\'m looking for a jQuery plugin that can get URL parameters, and support this search string without outputting the JavaScript error: \"malformed URI sequence\". If there is

相关标签:
19条回答
  • 2020-11-21 14:03

    What you really want is the jQuery URL Parser plugin. With this plugin, getting the value of a specific URL parameter (for the current URL) looks like this:

    $.url().param('foo');
    

    If you want an object with parameter names as keys and parameter values as values, you'd just call param() without an argument, like this:

    $.url().param();
    

    This library also works with other urls, not just the current one:

    $.url('http://allmarkedup.com?sky=blue&grass=green').param();
    $('#myElement').url().param(); // works with elements that have 'src', 'href' or 'action' attributes
    

    Since this is an entire URL parsing library, you can also get other information from the URL, like the port specified, or the path, protocol etc:

    var url = $.url('http://allmarkedup.com/folder/dir/index.html?item=value');
    url.attr('protocol'); // returns 'http'
    url.attr('path'); // returns '/folder/dir/index.html'
    

    It has other features as well, check out its homepage for more docs and examples.

    Instead of writing your own URI parser for this specific purpose that kinda works in most cases, use an actual URI parser. Depending on the answer, code from other answers can return 'null' instead of null, doesn't work with empty parameters (?foo=&bar=x), can't parse and return all parameters at once, repeats the work if you repeatedly query the URL for parameters etc.

    Use an actual URI parser, don't invent your own.

    For those averse to jQuery, there's a version of the plugin that's pure JS.

    0 讨论(0)
  • 2020-11-21 14:04

    After reading all of the answers I ended up with this version with + a second function to use parameters as flags

    function getURLParameter(name) {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)','i').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
    }
    
    function isSetURLParameter(name) {
        return (new RegExp('[?|&]' + name + '(?:[=|&|#|;|]|$)','i').exec(location.search) !== null)
    }
    
    0 讨论(0)
  • 2020-11-21 14:04

    You should not use jQuery for something like this!
    The modern way is to use small reusable modules through a package-manager like Bower.

    I've created a tiny module that can parse the query string into an object. Use it like this:

    // parse the query string into an object and get the property
    queryString.parse(unescape(location.search)).search;
    //=> æøå
    
    0 讨论(0)
  • 2020-11-21 14:08

    Need to add the i parameter to make it case insensitive:

      function getURLParameter(name) {
        return decodeURIComponent(
          (RegExp(name + '=' + '(.+?)(&|$)', 'i').exec(location.search) || [, ""])[1]
        );
      }
    
    0 讨论(0)
  • 2020-11-21 14:09

    This may help.

    <script type="text/javascript">
        $(document).ready(function(){
            alert(getParameterByName("third"));
        });
        function getParameterByName(name){
            var url     = document.URL,
                count   = url.indexOf(name);
                sub     = url.substring(count);
                amper   = sub.indexOf("&"); 
    
            if(amper == "-1"){
                var param = sub.split("=");
                return param[1];
            }else{
                var param = sub.substr(0,amper).split("=");
                return param[1];
            }
    
        }
    </script>
    
    0 讨论(0)
  • 2020-11-21 14:10

    Just in case you guys have the url like localhost/index.xsp?a=1#something and you need to get the param not the hash.

    var vars = [], hash, anchor;
    var q = document.URL.split('?')[1];
    if(q != undefined){
        q = q.split('&');
        for(var i = 0; i < q.length; i++){
            hash = q[i].split('=');
            anchor = hash[1].split('#');
            vars.push(anchor[0]);
            vars[hash[0]] = anchor[0];
        }
    }
    
    0 讨论(0)
提交回复
热议问题