How JS Gets the project path in a plain HTML page

Source: Internet
Author: User

JS, the full name of JavaScript, but although the beginning of Java, but not with Java a little relationship.

JS and Java have the following differences:

(1) JS is a browser-side language, while Java is the server-side language.

(2) JS is a dynamic language, Java is a static language.

(3) Java is based on object-oriented programming language, and JS has the concept of objects, but only based on objects but not directly inherit;

What do you mean by a bit of dynamic and static?

Simply put, the dynamic is not need to compile to be able to execute, and static need to compile execution.

Now the more popular is the front-end separation, if the use of pure HTML development +JS to obtain background data, then how to obtain the path?

Since it is a browser, it has to mention a web development of the most basic and most important thing, the HTTP protocol.

Here I would like to cite a friend's words:

HTTP requests have the following characteristics:

(1) Simple and fast: When a customer requests a service from the server, it simply transmits the request method and path. The request method commonly has, POST. Each method specifies a different type of contact between the customer and the server. Because the HTTP protocol is simple, the HTTP server's program size is small, so the communication speed is fast.

(2) Flexible: HTTP allows the transfer of any type of data object. The type being transmitted is marked by Content-type.

(3) No connection: The meaning of no connection is to limit the processing of only one request per connection. When the server finishes processing the customer's request and receives the customer's answer, the connection is disconnected. In this way, the transmission time can be saved.

(4) Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase. On the other hand, it responds faster when the server does not need the previous information.
(5) Support B/S and C/s mode.

There is also a concept to be mentioned here:

URL in an HTTP request

HTTP uses a Uniform Resource identifier (Uniform Resource Identifiers, URI) to transfer data and establish a connection. A URL is a special type of URI that contains enough information to find a resource

URL, full name is Uniformresourcelocator, Chinese is called the Uniform Resource Locator, is used on the Internet to identify a resource address. Take the following URL as an example to introduce the parts of the common URL:

Http://www.yc520.com:8080/news/index.asp?boardID=5&ID=24618&page=1#name

As you can see from the URL above, a complete URL includes the following sections:
(1) Part of the agreement: the protocol portion of the URL is "http:", which means that the Web page uses the HTTP protocol. You can use multiple protocols in the Internet, such as http,ftp, and so on, in this case the HTTP protocol. "//" after "HTTP" is a delimiter

(2) Domain name part: The domain name portion of the URL is "www.yc520.com". A URL, you can also use the IP address as the domain name

(3) Port section: followed by the domain name is the port, between the domain name and the port using ":" As the delimiter. The port is not a required part of the URL and if the port portion is omitted, the default port will be used

(4) Virtual directory part: From the first "/" after the domain name to the last "/", is the virtual directory part. The virtual directory is also not a required part of the URL. The virtual directory in this example is "/news/"

(5) File name part: From the last "/" after the domain name to "?" "So far, is the file name part, if there is no"? ", then from the domain name after the last"/"Start to" # "so far, is the document part, if not"? "and" # ", then from the last"/"after the domain name to the end, is the file name section. The file name in this example is "index.asp". The file name section is also not a required part of the URL, and if omitted, the default file name is used

(6) Anchor part: From the beginning of "#" to the end, are the anchor parts. The anchor section in this example is "name". The anchor part is also not a required part of the URL

(7) Parameter section: from "? The part between start and # is the parameter part, also called the search section, the query part. In this example, the parameter section is "Boardid=5&id=24618&page=1". Parameters can be allowed to have more than one parameter, with "&" as the delimiter between parameters and parameters.

Below we get to the point, how to get project path through JS?

The following JS code can be obtained:

/** * Project Path */window.onload=function () {    getpath ();} function GetPath () {    var pathName = document.location.pathname;    var index = PATHNAME.SUBSTR (1). IndexOf ("/");    var result = Pathname.substr (0,index+1);    $ ("#path"). Val (result);}

About JS This part, I will write a serial system in follow-up for everyone to introduce and explain JS. Share your experiences and summarize them.

How JS Gets the project path in a plain HTML page

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.