用javascript获取地址栏参数

      2020-08-02 10:33      编程语言
方法一:
<scripttype="text/javascript">
<!--
functionRequest(strName){
varstrHref="www.cnlei.org/index.htm?a=aaa&b=bbb&c=ccc";
varintPos=strHref.indexOf("?");
varstrRight=strHref.substr(intPos+1);
vararrTmp=strRight.split("&");
for(vari=0;i<arrTmp.length;i++){
vararrTemp=arrTmp[i].split("=");
if(arrTemp[0].toUpperCase()==strName.toUpperCase())returnarrTemp[1];
}
return"";
}

alert(Request("a"));
alert(Request("b"));
alert(Request("c"));
//-->
</script>

方法二:
<scripttype="text/javascript">
<!--
String.prototype.getQuery=function(name){
  varreg=newRegExp("(^|&)"+name+"=([^&]*)(&|$)");
  varr=this.substr(this.indexOf("\?")+1).match(reg);
  if(r!=null)returnunescape(r[2]);returnnull;
}
varstrHref="www.cnlei.org/index.htm?a=aaa&b=bbb&c=ccc";
alert(strHref.getQuery("a"));
alert(strHref.getQuery("b"));
alert(strHref.getQuery("c"));
//-->
</script>

方法三:
<scripttype="text/javascript">
functiongetUrlPara(paraName){
varsUrl=location.href;
varsReg="(?:\\?|&){1}"+paraName+"=([^&]*)"
varre=newRegExp(sReg,"gi");
re.exec(sUrl);
returnRegExp.$1;
}

//应用实例:test_para.html?a=11&b=22&c=33
alert(getUrlPara("a"));
alert(getUrlPara("b"));
</script>