Home > Web Front-end > JS Tutorial > Use javascript to get address bar parameters_javascript skills

Use javascript to get address bar parameters_javascript skills

WBOY
Release: 2016-05-16 19:22:48
Original
1268 people have browsed it

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template