Get escaped URL parameter

后端 未结 19 2672
小蘑菇
小蘑菇 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: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/

提交回复
热议问题