Obtain URL summary using JavaScript

Source: Internet
Author: User

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.

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.