首頁 >web前端 >js教程 >用javascript获取地址栏参数_javascript技巧

用javascript获取地址栏参数_javascript技巧

WBOY
WBOY原創
2016-05-16 19:22:481250瀏覽

方法一:
<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>

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn