I. Get the querystring parameter of the URL
Two ways to get the querystring parameter of a URL are as follows:
1.1 Method One: regular match
Gets the parameter
function getquerystring (name) {
var reg = new RegExp (^|&) "+ name +" = ([^&]*) (&|$) in the URL, i) ; Match target parameter
var result = Window.location.search.substr (1). Match (reg);//querystring matching target parameter
if (result!= null) C8/>return decodeuricomponent (result[2]);
else {return
null;
}
}
For Http://localhost/index.html?q1=abc&q2=efg&q3=h URLs, the method to get Q1 parameter values is as follows:
var q1 = getquerystring (' Q1 '); Abc
1.2 Method Two: Split
function getquerystring () {
var qs = location.search.substr (1),//Get URL "?" Character string
args = {},//Save parameter Data Objects
items = qs.length? Qs.split ("&"): [],//Get each argument item
= NULL,
len = Items.length;
for (var i = 0; i < len; i++) {
item = items[i].split ("=");
var name = decodeURIComponent (Item[0]),
value = decodeURIComponent (item[1));
if (name) {
Args[name] = value;
}
}
return args;
}
For Http://localhost/index.html?q1=abc&q2=efg&q3=h URLs, the method to get Q1 parameter values is as follows:
var qs = getquerystring ();
var q1 = qs["Q1"]; Abc
Both of the above getQueryString()
methods can solve the problem of getting the QueryString parameter of the URL well. In this way, arrange a location object to facilitate the future learning reference.
Two. Properties of the Location object
Note: Take Http://localhost:80/dir/index.html?q1=abc&q2=efg&q3=h#anchor as an example:
These 8 attributes of location are both writable and writable.
In which, the change location.href
jumps to the new URL page, and the modification location.hash
jumps to the anchor position in the current page.
Each modified window.location
property (except hash), the page is reloaded with a new URL and a record is generated in the browser's history.
Three. Methods of Location objects
location.assign(url)
The effect is exactly the same as the following two lines of code:
window.location = URL;
location.href = URL;
Code that is located location.reload()
after the call may or may not be executed, depending on factors such as network latency or system resources. Therefore, it is best to location.reload()
put it on the last line of code.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can help, if there is doubt you can message exchange.