URL: Uniform Resource Locator (URL)
The complete URL consists of these parts:
scheme://host:port/path?query#fragment
scheme = communication protocol (commonly used http, ftp, maito, etc.)
host = host (domain name or IP)
port = port number
path = path
query = query
Optionally, it is used to pass parameters to dynamic web pages (such as web pages made using CGI, ISAPI, PHP/JSP/ASP/ASP.NET and other technologies). There can be multiple parameters, separated by the "&" symbol, and the name and value of each parameter are separated by the "=" symbol.
fragment = information fragment
A string that specifies fragments in a network resource. For example, there are multiple noun explanations in a web page, and you can use fragment to directly locate a certain term explanation. (also known as anchor points.)
For such a URL
http://www.master8.net:80/seo/?ver=1.0&id=6#imhere
We can get the various parts of it in javascript
1, window.location.href
The entire URl string (the complete address bar in the browser)
2, window.location.protocol
The protocol part of the URL
This example returns the value: http:
3,window.location.host
The host part of the URL
Return value in this example: www.master8.net
4,window.location.port
The port part of the URL
If the default 80 port is used (update: even if:80 is added), the return value is not the default 80 but the empty character
This example returns the value: ""
5, window.location.pathname
The path part of the URL (that is the file address)
This example returns the value: /seo/
6,window.location.search
Query (parameters) part
In addition to assigning values to dynamic languages, we can also give static pages and use javascript to obtain the value of the parameter that is believed to be.
Return value in this example:?ver=1.0&id=6
7,window.location.hash
Anchor point
Return value in this example: #imhere
8. URL parameter value
Method 1: Regular Analysis Method
The code copy 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;
}
Method 2: Use split to split into an array
The code copy is as follows:
function GetRequest() {
var url = location.search; //Get the string after the "?" character in the 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;
}
The method is very simple, but it is very practical. Here are two commonly used methods. If you have different methods, please tell me. This article continues to be updated. Everyone makes progress together