Get escaped URL parameter

后端 未结 19 2674
小蘑菇
小蘑菇 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 13:45

    Below is what I have created from the comments here, as well as fixing bugs not mentioned (such as actually returning null, and not 'null'):

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

    For example , a function which returns value of any parameters variable.

    function GetURLParameter(sParam)
    {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++)
        {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam)
            {
                return sParameterName[1];
            }
        }
    }​
    

    And this is how you can use this function assuming the URL is,

    "http://example.com/?technology=jquery&blog=jquerybyexample".

    var tech = GetURLParameter('technology');
    var blog = GetURLParameter('blog');
    

    So in above code variable "tech" will have "jQuery" as value and "blog" variable's will be "jquerybyexample".

    0 讨论(0)
  • 2020-11-21 13:51
    <script type="text/javascript">
    function getURLParameter(name) {
            return decodeURIComponent(
                (location.search.toLowerCase().match(RegExp("[?|&]" + name + '=(.+?)(&|$)')) || [, null])[1]
            );
        }
    
    </script>
    

    getURLParameter(id) or getURLParameter(Id) Works the same : )

    0 讨论(0)
  • 2020-11-21 13:53

    There's a lot of buggy code here and regex solutions are very slow. I found a solution that works up to 20x faster than the regex counterpart and is elegantly simple:

        /*
        *   @param      string      parameter to return the value of.
        *   @return     string      value of chosen parameter, if found.
        */
        function get_param(return_this)
        {
            return_this = return_this.replace(/\?/ig, "").replace(/=/ig, ""); // Globally replace illegal chars.
    
            var url = window.location.href;                                   // Get the URL.
            var parameters = url.substring(url.indexOf("?") + 1).split("&");  // Split by "param=value".
            var params = [];                                                  // Array to store individual values.
    
            for(var i = 0; i < parameters.length; i++)
                if(parameters[i].search(return_this + "=") != -1)
                    return parameters[i].substring(parameters[i].indexOf("=") + 1).split("+");
    
            return "Parameter not found";
        }
    
    console.log(get_param("parameterName"));
    

    Regex is not the be-all and end-all solution, for this type of problem simple string manipulation can work a huge amount more efficiently. Code source.

    0 讨论(0)
  • 2020-11-21 13:54

    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;
        }
        else{
           return 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)
提交回复
热议问题