Home >Web Front-end >JS Tutorial >Detailed explanation of Javascript parsing URL method_javascript skills
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
Optional, 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 "&" symbols, each parameter The name and value are separated by the "=" symbol.
fragment = information fragment
A string used to specify the fragment in the network resource. For example, if there are multiple noun explanations in a web page, you can use fragment to directly locate a certain noun explanation. (Also called anchor point.)
For such a URL
http://www.master8.net:80/seo/?ver=1.0&id=6#imhere
We can use javascript to get various parts of it
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
The return value in this example: http:
3,window.location.host
The host part of the URL
The return value in this example: www.master8.net
4,window.location.port
The port part of the URL
If the default port 80 is used (update: even if :80 is added), the return value is not the default 80 but the empty character
The return value in this example:""
5,window.location.pathname
The path part of the URL (that is, the file address)
The return value in this example:/seo/
6,window.location.search
Query (parameter) part
In addition to assigning values to dynamic languages, we can also assign values to static pages and use javascript to obtain the corresponding parameter values
The return value in this example:?ver=1.0&id=6
7,window.location.hash
Anchor
Return value in this example: #imhere
8. url parameter value
Method 1: Regular Analysis
Method 2: Split into arrays
The method is very simple, but very practical. Here are 2 commonly used methods. If you have different methods, please let us know. This article will continue to be updated. Let’s make progress together