JavaScript Get URL Rollup

Source: Internet
Author: User
Tags hash port number

In web development, many developers prefer to use JavaScript to get the current URL URLs, this article for you to summarize the more commonly used to get the URL of the JavaScript implementation code

The URL is the Uniform Resource Locator (uniform Resource Locator, URL), and the complete URL consists of the following parts:

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

Scheme: Communication protocols, commonly used Http,ftp,maito, etc.

Host: Hosts, server (computer) domain Name System (DNS) hostname or IP address.

Port: Port number, integer, optional, default port for use scheme when omitted, such as HTTP's default port is 80.

Path: A string, separated by 0 or more "/" symbols, that is typically used to represent a directory or file address on a host.

Query: Queries, optional, for dynamic Web pages, such as using CGI, ISAPI, php/jsp/asp/asp. NET, and other technology-made pages, can have multiple parameters, separated by the "&" symbol, and the name and value of each parameter are separated by the "=" symbol.

Fragment: A piece of information, a string, used to specify a fragment in a network resource. For example, a Web page has multiple noun interpretations, which can be directly positioned to a noun interpretation using fragment. (also called Anchor Point)

Here's an example of a URL, and then get the various components of it.

Http://www.jb51.net/newsDetail.php?id=65

Window.location.href

You can get the entire URL string (which is the complete address bar in the browser).

?

1 2 var test = window.location.href; alert (test);

Program returns to Http://www.jb51.net/newsDetail.php?id=65

Window.location.protocol

The protocol portion of the URL can be obtained

?

1 2 var test = Window.location.protocol; alert (test);

Program returns http:

Window.location.host

Can get the host part of the URL

?

1 2 var test = Window.location.host; alert (test);

Program returns to Www.jb51.net

Window.location.port

Can get the port portion of the URL

?

1 2 var test = Window.location.port; alert (test);

If you use the default port 80 (update: Even if you added: 80), the return value is not the default 80 but the null character.

Window.location.pathname

Get the path part of the URL (that is, the file address)

?

1 2 var test = Window.location.pathname; alert (test);

Window.location.search

To get the query (parameter) section, in addition to assigning values to dynamic languages, we can also give static pages and use JavaScript to get the value of the arguments we believe they should be.

?

1 2 var test = Window.location.search; alert (test);

Window.location.hash

Get anchor points.

?

1 2 var test = Window.location.hash; alert (test);

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Article

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.