JS get URL parameter value of a lot of methods, the following also for you to introduce two, like friends can test, hope to be helpful to everyone
method One: The regular analysis Method
The code is as follows:
function getquerystring (name) {
var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)", "I");
var r = window.location.search.substr (1). Match (REG);
if (r! = null) return unescape (r[2]); return null;
}
Call Method:
Alert (getquerystring ("parameter name 1")), Alert (getquerystring ("parameter Name 2"));
Alert (getquerystring ("parameter name 3"));
Method Two
The code is as follows:
<script language= "JavaScript" >
function Getrequest () {
var url = location.search; Gets the URL in the "?" String after the character
var therequest = new Object ();
if (Url.indexof ("?")! =-1) {
var str = URL.SUBSTR (1);
STRs = Str.split ("&");
for (var i = 0; i < strs.length; i + +) {
Therequest[strs[i].split ("=") [0]]=unescape (Strs[i].split ("=") [1]);
}
}
return therequest;
}
</Script>
Call Method:
<script language= "JavaScript" >
var Request = new Object ();
Request = Getrequest ();
var parameter 1, Parameter 2, parameter 3, parameter n;
Parameter 1 = request[' parameter 1 '];
Parameter 2 = request[' parameter 2 '];
Parameter 3 = request[' parameter 3 '];
Parameter n = request[' parameter n '];
</Script>
JS processing url Practical Tips
Escape (), encodeURI (), encodeURIComponent () Three methods can filter some special characters that affect URL integrity.
But the latter two is the way to convert the string to UTF-8, which solves the problem of garbled page encoding.
For example: The sending page is inconsistent with the encoding format (Charset) of the Accept page (assuming that the sending page is GB2312 and the receiving page encoding is UTF-8), and using the escape () conversion will cause garbled problems with the text string in transit.
Here are the various ways to encode/decode URLs under JS:
Escape method: For @*+/A-Z 0-9 A-Z These characters are not encoded, other non-ASCII characters are encoded to%XX encoding substitution, decoding using the Unescape,escape method cannot be used to encode a "Uniform Resource Identifier" (URI). The encoding should use the encodeURI and encodeURIComponent methods. encodeURI (): Yes! @ # $ & * () =:/;? + ' A-Z 0-9 A-Z, other characters will be encoded, decoded using decodeURI ();
if you want to make more words to be encoded, for example:/, use the encodeURIComponent () method, after being encoded by this method, the parameter passed to the server is an invalid character, decoding using decodeuricomponent ()
JS get URL parameter values in two ways