Obtain URL summary using JavaScript
In WEB development, many developers prefer to use javascript to obtain the current url. This article summarizes the javascript implementation code that is commonly used to obtain the URL.
The URL is the Uniform Resource Locator (Uniform Resource Locator, URL). The complete URL consists of the following parts:
Scheme: // host: port/path? Query # fragment
Scheme: communication protocol, common http, ftp, maito, etc.
Host: host, server (Computer) Domain Name System (DNS) host Name or IP address.
Port: port number, which is an integer. (optional) Default port used when the scheme is omitted. For example, the default port of http is 80.
Path: path, which is a string separated by zero or multiple "/" symbols. It is generally used to represent a directory or file address on the host.
Query: query (optional) used for dynamic web pages (for example, CGI, ISAPI, PHP/JSP/ASP. NET and other technologies to produce web pages) transmission parameters, can have multiple parameters, separated by the "&" symbol, each parameter name and value are separated by the "=" symbol.
Fragment: Information fragment, a string used to specify fragments in network resources. For example, if a webpage contains multiple glossary, you can use fragment to directly locate the glossary. (Also called anchor)
The following example shows a URL and obtains its components.
Http://www.jb51.net/newsDetail.php? Id = 65
Window. location. href
You can obtain the entire URL string (the complete address bar in the browser ).
?
| 1 2 |
Var test = window. location. href; Alert (test ); |
Program return http://www.jb51.net/newsDetail.php? Id = 65
Window. location. protocol
Obtain the URL protocol section.
?
| 1 2 |
Var test = window. location. protocol; Alert (test ); |
The program returns http:
Window. location. host
Obtain the host part of the URL.
?
| 1 2 |
Var test = window. location. host; Alert (test ); |
The program returns www.jb51.net
Window. location. port
Obtain the URL Port
?
| 1 2 |
Var test = window. location. port; Alert (test ); |
If the default port 80 is used (update: Even if port 80 is added), the return value is not the default port 80, but a null character.
Window. location. pathname
Obtain the URL path (that is, the file address)
?
| 1 2 |
Var test = window. location. pathname; Alert (test ); |
Window. location. search
In addition to assigning values to dynamic languages, we can also give static pages and use javascript to obtain expected parameter values.
?
| 1 2 |
Var test = window. location. search; Alert (test ); |
Window. location. hash
Obtain the anchor.
?
| 1 2 |
Var test = window. location. hash; Alert (test ); |
The above is all the content of this article. I hope you will like it.