For URLs, we need to know more because we might need to extract some of the information from the URL to do something different, in fact it's a unique way of interacting with the backend, which is certainly safe when the request is returned, the information about the URL is recorded in the Window object's In a Location object, it is important that the result of the value does not change as the user manually modifies the characters in the Address bar. To understand this, let's start with the following picture:
The long string in the middle of the picture is a full URL that contains any part of a URL that might contain: protocol, domain name, port number (of course, in most cases, we do not see the port number when browsing the Web page, because he is hidden, the default is 80 port, you add will not be wrong), path, parameter, stroke. In JavaScript, the way to get to this line of string is to access the Window.location.href,href property that contains the full URL of a page. If you want to get the value of a part of the URL, we can parse the full URL through a complex and tedious regular expression, but it's more convenient to get it through the other properties of the location. For example, the protocol attribute in location records the protocol name with a colon, and the pathname attribute stores the path name, which is the separation of the href, which brings a lot of convenience to the developer. The table below lists all the properties under location, along with their respective recorded values. If the contents of the table cannot be understood, then the color blocks and text in the above image depict the corresponding location of each property in the URL.
Properties |
value |
Href |
The full URL |
Protocol |
Agreement |
Hostname |
Host Name |
Host |
Host name plus port number |
Port |
The port number |
Pathname |
The path portion of the current URL |
Search |
Query part of URL |
Hash |
#开始的锚 |
It is important to note that the above properties are optional, which means that we can change their values through JavaScript. Most of these properties are very simple at a glance, only the search part is more troublesome, the search part is sent to the background parameters, to? Starting with,& as a delimiter, = Assigning a serialized string, so that the value obtained by Location.search is often not the final result to be obtained, and through the structure of search, we may want to get a result that is an associative array with a definite correspondence. So we need to do further processing of the strings that we get in Location.search.
12345678910111213141516171819 |
function getArgs() {
var
args = {};
var
query = location.search.substring(1);
// Get query string
var
pairs = query.split(
"&"
);
// Break at ampersand
for
(
var
i = 0; i < pairs.length; i++) {
var
pos = pairs[i].indexOf(
‘=‘
);
// Look for "name=value"
if
(pos == -1)
continue
;
// If not found, skip
var argname = pairs[i].substring(0,pos);
// Extract the name
var
value = pairs[i].substring(pos+1);
// Extract the value
value = decodeURIComponent(value);
// Decode it, if needed
args[argname] = value;
// Store as a property
}
return
args;
// Return the object
}
|
The code above comes from the "JavaScript authoritative guide" book. The Getargs method does not receive parameters, it actively extracts the search part of the URL to parse it, and returns a JSON. For example, the URL in the image we started with, using the Getargs method will get the following result:
123 |
var search = { search:html } |
In this way, all information about the URL can get a very clear result, very simple.
However, it is more recommended to use regular expressions to parse URLs, which are more efficient and more concise.
12345678910 |
function
getArgs(){
var
args = {};
var match =
null
;
var
search = decodeURIComponent(location.search.substring(1));
var
reg = /(?:([^&]+)=([^&]+))/g;
while
((match = reg.exec(search))!==
null
){
args[match[1]] = match[2];
}
return
args;
}
|
Original link: http://www.jsann.com/post/JS_GET_parameters_to_obtain.html
JS Get Get Parameters