This article mainly introduced the JS obtains the URL to pass the value the method, the example analyzed the string segmentation and the regular analysis two methods, and supplemented a regular match realization The JS obtains the URL The Get function, needs the friend can refer to the next
JS gets the URL parameter value:
Index.htm? parameter 1 = numeric 1& parameter 2 = numeric 2& parameter 3 = data 3& parameter 4 = numeric 4&
Static HTML file JS read URL parameter control HTML page output based on the parameter value of Get HTML
One, the string segmentation analysis method
Here is a JavaScript client solution that gets the URL with the questring parameter, equivalent to the ASP's request.querystring,php $_get
Function:
1234567891011121314 |
<Script language=
"javascript"
>
function GetRequest() {
var url = location.search;
//获取url中"?"符后的字串
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>
|
Then we get the corresponding parameter value by calling this function:
123456789 |
<script language= "javascript" > var request = new object (); request = Getrequest (); var parameter 1 = request[ ]; parameter 2 = request[ ]; parameter 3 = request[ ]; parameter n = request[ ]; </SCRIPT> |
This gets the parameter with the same name in the URL string
Second, the regular analysis method
12345678 |
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
;
}
alert(GetQueryString(
"参数名1"
));
alert(GetQueryString(
"参数名2"
));
alert(GetQueryString(
"参数名3"
));
|
Add: JS Gets the URL of the Get Pass function
123456 |
function getvl(name) {
var reg =
new RegExp(
"(^|\\?|&)"
+ name +
"=([^&]*)(\\s|&|$)"
,
"i"
);
if (reg.test(location.href))
return unescape(RegExp.$2.replace(/\+/g,
" "
));
return ""
;
};
|
How to get URL value by JS