How to obtain the browser address and parameters in javascript

Source: Internet
Author: User

Use javascript to obtain url Information

Run the following javascript code to obtain url Information:

Location. host = www. Dw. Cn

Location. hostname = www. Dw. Cn

Location. href = http: // www. Dw. Cn/news/2010-1/201011820131610618. shtml

Location. pathname =/news/2010-1/201011820131610618. shtml

Location. protocol = http:

Detailed introduction to the window. location Method for obtaining URLs

The complete URL of the unified Resource Locator (Uniform Resource Locator, URL) consists of the following parts:

Scheme: // host: port/path? Query # fragment

Scheme: Communication Protocol

Common http, ftp, maito, etc.

Host: host

The host name or IP address of the server (Computer) Domain Name System (DNS.

Port: port Number

INTEGER (optional) Default port used when the scheme is omitted. For example, the default port of http is 80.

Path: path

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 (such as CGI, ISAPI, PHP/JSP/ASP. NET and other technologies) Transfer Parameters. Multiple parameters can be separated by the "&" symbol. The names and values of each parameter are separated by the "=" symbol.

Fragment: Information fragment

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 .)

Example:

1, window. location. href

The whole URl string (the complete address bar in the browser)

Returned value: http: // www. Dw. Cn: 80/index. asp? Id = 209 # asp

2, window. location. protocol

URL protocol section

Returned value: http:

3, window. location. host

Host part of the URL,

Return Value: www. Dw. Cn

4, window. location. port

The port of the URL. 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.

Returned value in this example: NULL

5, window. location. pathname

URL path (that is, the file address)

Returned value:/index. asp

6. window. location. search

Query (parameter) section. In addition to assigning values to dynamic languages, we can also assign values to static pages and use javascript to obtain expected parameter values.

Returned value :? Id = 209

7. window. location. hash

Anchor

Returned value: # asp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.