Home > Web Front-end > JS Tutorial > body text

Use JavaScript to get the URL address of the current page

高洛峰
Release: 2016-11-28 13:46:39
Original
1373 people have browsed it

URL is: 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
server (computer) domain name system (DNS) host name or IP address.

port: Port number
integer, optional. When omitted, the default port of the scheme is used. For example, the default port of http is 80.

path: Path
A string separated by zero or more "/" symbols, generally used to represent a directory or file address on the host.

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, use the "&" symbol Separate, the name and value of each parameter are separated by "=" symbols.

fragment: information fragment
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 known as an anchor.)

For a URL like this

http://www.x2y2.com0/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere


We can use javascript to obtain each part of it
1, window.location.href
The entire URL string (the complete address bar in the browser)
The return value in this example: http://www.x2y2.com0/ fisker/post/0703/window.location.html?ver=1.0&id=6#imhere


2, the protocol part of window.location.protocol
URL
The return value in this example: http/div>

3 , the host part of window.location.host
URL
The return value in this example: www.x2y2.com

4, the port part of window.location.port
URL
If the default port 80 is used (update: even if 80 is added ), then the return value is not the default 80 but a null 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:/fisker/ post/0703/window.location.html

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
The return value in this example is imhere


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template