Obtain the URL and parameters of a webpage using JavaScript.

Source: Internet
Author: User

First look at an example

Use JavaScript to obtain URL Information

Java code

<script type="text/javascript">   document.write("location.host="+location.host+"<br>");   document.write("location.hostname="+location.hostname+"<br>");   document.write("location.href="+location.href+"<br>");   document.write("location.pathname="+location.pathname+"<br>");   document.write("location.protocol="+location.protocol+"<br>");   </script>  <script type="text/javascript">document.write("location.host="+location.host+"<br>");document.write("location.hostname="+location.hostname+"<br>");document.write("location.href="+location.href+"<br>");document.write("location.pathname="+location.pathname+"<br>");document.write("location.protocol="+location.protocol+"<br>");</script>

Run the following JavaScript code to obtain URL Information:

Java code

location.host=www.vnde.cn   location.hostname=www.vnde.cn   location.href=http://www.vnde.cn/bc/2008/0306/article_1860.html   location.pathname=/bc/2008/0306/article_1860.html   location.protocol=http:  location.host=www.vnde.cnlocation.hostname=www.vnde.cnlocation.href=http://www.vnde.cn/bc/2008/0306/article_1860.htmllocation.pathname=/bc/2008/0306/article_1860.htmllocation.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.2astudio.com: 80/view. asp? Id = 209 # cmt1323

2, window. Location. Protocol
URL protocol section
Returned value: http:

3, window. Location. Host
Host part of the URL,
Returned value: www.2astudio.com

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:/view. 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: # cmt1323

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.