Skip to content

获得URL参数值 #3

Description

@cherislive
  /*
   * 获得URL参数值
   * @param   {String}  URL 地址  // url = window.location;
   * @param   {String}  参数名 可传
   * @return  {String}  返回结果
  */
getQueryString = function (name){
      var pattern    = new RegExp("[?&]" + name +"\=([^&]+)","g");
      var patternDef = new RegExp("[\?\&][^\?\&]+=[^\?\&]+","g");
      var matcher    = pattern.exec(window.location.href);
      var items      = null;
      if(name && matcher != null){
          try{
              items = decodeURIComponent(decodeURIComponent(matcher[1]));   
          }catch(e){
              try{
                  items = decodeURIComponent(matcher[1]);
              }catch(e){
                  items = matcher[1];
              }
          }
      }else{
        patternDef = location.search.match(patternDef);
        if(patternDef != null){
          var tmpArray;
          items = {};
          for(var i = 0; i < patternDef.length; i++){
             tmpArray = patternDef[i].substring(1).split('=');
             items[tmpArray[0]] = tmpArray[1];
          }
        }
      }
      return items;
  };

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions