Heim > Artikel > Web-Frontend > 用javascript获取地址栏参数_javascript技巧
方法一:
<script> <BR><!-- <BR>function Request(strName){ <BR> var strHref = "www.cnlei.org/index.htm?a=aaa&b=bbb&c=ccc"; <BR> var intPos = strHref.indexOf("?"); <BR> var strRight = strHref.substr(intPos + 1); <BR> var arrTmp = strRight.split("&"); <BR> for(var i = 0; i < arrTmp.length; i++) { <BR> var arrTemp = arrTmp[i].split("="); <BR> if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1]; <BR> } <BR> return ""; <BR>} <br><br>alert(Request("a")); <BR>alert(Request("b")); <BR>alert(Request("c")); <BR>//--> <BR></script>
方法二:
<script> <BR><!-- <BR>String.prototype.getQuery = function(name) { <BR> var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); <BR> var r = this.substr(this.indexOf("\?")+1).match(reg); <BR> if (r!=null) return unescape(r[2]); return null; <BR>} <BR>var strHref = "www.cnlei.org/index.htm?a=aaa&b=bbb&c=ccc"; <BR>alert(strHref.getQuery("a")); <BR>alert(strHref.getQuery("b")); <BR>alert(strHref.getQuery("c")); <BR>//--> <BR></script>
方法三:
<script> <BR>function getUrlPara(paraName){ <BR> var sUrl = location.href; <BR> var sReg = "(?:\\?|&){1}"+paraName+"=([^&]*)" <BR> var re=new RegExp(sReg,"gi"); <BR> re.exec(sUrl); <BR> return RegExp.$1; <BR>} <br><br>//应用实例:test_para.html?a=11&b=22&c=33 <BR>alert(getUrlPara("a")); <BR>alert(getUrlPara("b")); <BR></script>