Home > Article > Web Front-end > Javascript - HTML request class_javascript skills
When doing Chinese-English conversion, it is necessary to obtain the parameters accurately and take them out, so I made a simple HTML to use js to get an Object from the address bar.
There are three methods in it:
1. request.QueryString("Parameter")//Get the specified parameters and return a string;
2. request.QueryStrings();//Get all parameters and return Array;
3. request.setQuery("Parameter", "Parameter value"); //If the current address bar has this parameter, then this parameter will be updated, otherwise a new address bar parameter string will be returned.
For example:
The current address bar parameter string is:?name=a&site=never_online
alert(request.setQuery("name","blueDestiny"))
If If there is "name" in the address bar parameter, then return?name=blueDestiny&site=never_online
The setQuery method has the function of automatically appending parameters. For example:
The current address bar parameter string is:?site=never_online
alert(request.setQuery("name","blueDestiny"))
Then return ?site=never_online&name=blueDestiny
Similarly, if there are no parameters in the address bar, parameters will be automatically added
alert(request.setQuery("name","blueDestiny"))
Return?name=blueDestiny
<script> <BR><!-- <BR>// author: never-online <BR>// web: never-online.net <BR>var request = { <BR> QueryString : function(val) { <BR> var uri = window.location.search; <BR> var re = new RegExp("" +val+ "\=([^\&\?]*)", "ig"); <BR> return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null); <BR> }, <BR> QueryStrings : function() { <BR> var uri = window.location.search; <BR> var re = /\w*\=([^\&\?]*)/ig; <BR> var retval=[]; <BR> while ((arr = re.exec(uri)) != null) <BR> retval.push(arr[0]); <BR> return retval; <BR> }, <BR> setQuery : function(val1, val2) { <BR> var a = this.QueryStrings(); <BR> var retval = ""; <BR> var seted = false; <BR> var re = new RegExp("^" +val1+ "\=([^\&\?]*)$", "ig"); <BR> for(var i=0; i<a.length; i++) { <BR> if (re.test(a[i])) { <BR> seted = true; <BR> a[i] = val1 +"="+ val2; <BR> } <BR> } <BR> retval = a.join("&"); <BR> return "?" +retval+ (seted ? "" : (retval ? "&" : "") +val1+ "=" +val2); <BR> } <BR>} <BR>alert(request.setQuery("e","b")) <BR>//--> <BR></script>