Javascript-html's request class _javascript skills

Source: Internet
Author: User
To do the conversion of the Chinese and English, to get the exact parameters and take out, so do a simple HTML in the use of JS to get the address bar of an object.
There are three ways:
1, request. QueryString ("Parameters")//Gets the specified parameter, returns a string;
2, request. Querystrings ()//Get all the parameters and return the array;
3, Request.setquery ("parameter", "value of the parameter");//If this argument is present in the current address bar, this parameter is updated, otherwise a new Address bar argument string is returned.
For example:
The current address bar parameter string is:? name=a&site=never_online

Alert (request.setquery ("name", "Bluedestiny"))

If "name" is in the Address bar argument, then return? name=bluedestiny&site=never_online

The Setquery method has the function of automatically appending parameters. Such as:
The current address bar parameter string is:? site=never_online
Alert (request.setquery ("name", "Bluedestiny"))
Then return? site=never_online&name=bluedestiny

Similarly, if the address bar has no parameters, the parameters are automatically appended
Alert (request.setquery ("name", "Bluedestiny"))
Back? name=bluedestiny

<script language= "JavaScript" >
<!--
Author:never-online
Web:never-online.net
var request = {
Querystring:function (val) {
var uri = Window.location.search;
var re = new RegExp ("+val+" \=) ([^\&\?] *) "," IG ");
Return (Uri.match (re))? ( Uri.match (re) [0].substr (val.length+1)): null);
},
Querystrings:function () {
var uri = Window.location.search;
var re =/\w*\= ([^\&\?] *)/ig;
var retval=[];
while ((arr = re.exec (URI))!= null)
Retval.push (Arr[0]);
return retval;
},
Setquery:function (Val1, Val2) {
var a = this. Querystrings ();
var retval = "";
var seted = false;
var re = new RegExp ("^" +val1+ "\=") ([^\&\?] *) $ "," IG ");
for (var i=0; i<a.length; i++) {
if (Re.test (A[i])) {
Seted = true;
A[i] = val1 + "=" + val2;
}
}
retval = A.join ("&");
Return "?" +retval+ (seted?) "": (retval?) & ":") +val1+ "=" +val2);
}
}
Alert (Request.setquery ("E", "B"))
-->
</SCRIPT>

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.