Get escaped URL parameter

后端 未结 19 2707
小蘑菇
小蘑菇 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:54
    function getURLParameters(paramName) 
    {
            var sURL = window.document.URL.toString();  
        if (sURL.indexOf("?") > 0)
        {
           var arrParams = sURL.split("?");         
           var arrURLParams = arrParams[1].split("&");      
           var arrParamNames = new Array(arrURLParams.length);
           var arrParamValues = new Array(arrURLParams.length);     
           var i = 0;
           for (i=0;i<arrURLParams.length;i++)
           {
            var sParam =  arrURLParams[i].split("=");
            arrParamNames[i] = sParam[0];
            if (sParam[1] != "")
                arrParamValues[i] = unescape(sParam[1]);
            else
                arrParamValues[i] = "No Value";
           }
    
           for (i=0;i<arrURLParams.length;i++)
           {
                    if(arrParamNames[i] == paramName){
                //alert("Param:"+arrParamValues[i]);
                    return arrParamValues[i];
                 }
           }
           return "No Parameters Found";
        }
    
    }
    
    0 讨论(0)
  • 2020-11-21 13:55

    If you don't know what the URL parameters will be and want to get an object with the keys and values that are in the parameters, you can use this:

    function getParameters() {
      var searchString = window.location.search.substring(1),
          params = searchString.split("&"),
          hash = {};
    
      if (searchString == "") return {};
      for (var i = 0; i < params.length; i++) {
        var val = params[i].split("=");
        hash[unescape(val[0])] = unescape(val[1]);
      }
      return hash;
    }
    

    Calling getParameters() with a url like /posts?date=9/10/11&author=nilbus would return:

    {
      date:   '9/10/11',
      author: 'nilbus'
    }
    

    I won't include the code here since it's even farther away from the question, but weareon.net posted a library that allows manipulation of the parameters in the URL too:

    • Blog post: http://blog.weareon.net/working-with-url-parameters-in-javascript/
    • Code: http://pastebin.ubuntu.com/1163515/
    0 讨论(0)
  • 2020-11-21 13:56

    I created a simple function to get URL parameter in JavaScript from a URL like this:

    .....58e/web/viewer.html?page=*17*&getinfo=33
    
    
    function buildLinkb(param) {
        var val = document.URL;
        var url = val.substr(val.indexOf(param))  
        var n=parseInt(url.replace(param+"=",""));
        alert(n+1); 
    }
    buildLinkb("page");
    

    OUTPUT: 18

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

    You can use the browser native location.search property:

    function getParameter(paramName) {
      var searchString = window.location.search.substring(1),
          i, val, params = searchString.split("&");
    
      for (i=0;i<params.length;i++) {
        val = params[i].split("=");
        if (val[0] == paramName) {
          return unescape(val[1]);
        }
      }
      return null;
    }
    

    But there are some jQuery plugins that can help you:

    • query-object
    • getURLParam
    0 讨论(0)
  • 2020-11-21 13:57

    Slight modification to the answer by @pauloppenheim , as it will not properly handle parameter names which can be a part of other parameter names.

    Eg: If you have "appenv" & "env" parameters, redeaing the value for "env" can pick-up "appenv" value.

    Fix:

    var urlParamVal = function (name) {
        var result = RegExp("(&|\\?)" + name + "=(.+?)(&|$)").exec(location.search);
        return result ? decodeURIComponent(result[2]) : "";
    };
    
    0 讨论(0)
  • 2020-11-21 14:02

    Based on the 999's answer:

    function getURLParameter(name) {
        return decodeURIComponent(
            (location.search.match(RegExp("[?|&]"+name+'=(.+?)(&|$)'))||[,null])[1]
        );  
    }
    

    Changes:

    • decodeURI() is replaced with decodeURIComponent()
    • [?|&] is added at the beginning of the regexp
    0 讨论(0)
提交回复
热议问题