Get Address bar parameters with JavaScript

Source: Internet
Author: User
Tags return split

Core Tip: This article lists three ways to get address bar parameters with JavaScript

This article lists three ways to get address bar parameters using javascript:


Method One:


<script type= "Text/javascript" >
<!--
String.prototype.getQuery = function (name) {
var reg = new RegExp ("(^&)" + name + "= ([^&]*) (&$)");
var r = this.substr (This.indexof ("\?") +1). Match (REG);
if (r!=null) return unescape (r[2]); return null;
}
var strhref = "WWW.CNLEI.ORG/INDEX.HTM?A=AAA&B=BBB&C=CCC";
Alert (Strhref.getquery ("a"));
Alert (Strhref.getquery ("B"));
Alert (Strhref.getquery ("C"));
-->
</script>


Method Two:


<script type= "Text/javascript" >
function Geturlpara (paraname) {
var surl = location.href;
var sreg = "(?: \ \?&) {1} "+paraname+" = ([^&]*)
var re=new RegExp (Sreg, "GI");
Re.exec (sURL);
return regexp.$1;
}
Application Example: test_para.html?a=11&b=22&c=33
Alert (Geturlpara ("a"));
Alert (Geturlpara ("B"));
</script>


Method Three:


<script type= "Text/javascript" >
<!--
function Request (strName) {
var strhref = "WWW.CNLEI.ORG/INDEX.HTM?A=AAA&B=BBB&C=CCC";
var intpos = Strhref.indexof ("?");
var strright = strhref.substr (intpos + 1);
var arrtmp = Strright.split ("&");
for (var i = 0; i < arrtmp.length; i++) {
var arrtemp = arrtmp[i].split ("=");
if (arrtemp[0].touppercase () = = Strname.touppercase ()) return arrtemp[1];
}
Return "";
}
Alert (Request ("a"));
Alert (Request ("B"));
Alert (Request ("C"));
-->
</script>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.