小小程序员-魔法师

拥有魔法力量的程序员

JS获取浏览器地址参数的封装类

<script type="text/javascript" language="JavaScript">
function UrlProperty(url_pro){
 var tmpurl = url_pro;
 if(tmpurl){
  var tempurl = url_pro;
  while(true){
   tempurl = unescape(tempurl);
   if(tempurl == tmpurl){
    break;
   }else{
    tmpurl = tempurl;
   }
  }
 }
 else{
  var tempurl = window.location.href.toString();
  while(true){
   tempurl = unescape(tempurl);
   if(tempurl == tmpurl){
    break;
   }else{
    tmpurl = tempurl;
   }
  }

 }
 this.URL = tmpurl;
 this.substring = this.URL.indexOf("?")>0?this.URL.substring(this.URL.indexOf("?")+1,this.URL.length):"";
 this.speak = function(){alert(this.substring);};
 this.keywords = new Array();
 this.datas = new Array();
 var tmp = this.substring.split("&");
 this.get = function(key){
          for(var i = 0; i < this.keywords.length; i++){
           if(this.keywords[i] == key)
            return this.datas[i];
          }
          return null;
         }
 //根据属性获值
 this.isExist = function(key){
         for(var i = 0; i < this.keywords.length; i++){
          if(this.keywords[i] == key)
           return true;
         }
         return false;
        };
 //判断当前url中是否有该属性
 this.keyIndexOf = function(key){
          for(var i = 0; i < this.keywords.length; i++){
          if(this.keywords[i] == key)
           return i;
         }
         return -1;
        };
 //private
 this.getKeys = function(){return this.keywords;};
 //获取当前URL中的属性列表
 if(this.substring == "undefined" || this.substring == ""){

 }else{
  for(var i = 0; i < tmp.length; i++){
   var key = tmp[i].substring(0,tmp[i].indexOf("="));
   var data = tmp[i].indexOf("=")>0?tmp[i].substring(tmp[i].indexOf("=")+1,tmp[i].length):"";
   if(this.isExist(key)){
    var index = this.keyIndexOf(key);
    if(Object.prototype.toString.apply(this.datas[index]) === "[object Array]"){
     this.datas[index].push(data);
    }else{
     var temp = this.datas[index];
     this.datas[index] = new Array();
     this.datas[index].push(temp);
     this.datas[index].push(data);
    }
   }else{
    this.datas[i] = data;
   }
   this.keywords[i] = key;
  }
 }
}

var ap = new UrlProperty();

ap.get("keyword");
</script>

 

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zyb134506/article/details/5652759
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭