About absolute paths and relative paths

Source: Internet
Author: User
Tags httpcontext relative

I. Basic CONCEPTS

1, relative path-as the name suggests, relative path is relative to the current file path. The Web page generally indicates that the path uses this method.

2, absolute path-absolute path is your homepage on the file or directory on the hard drive real path. The absolute path is the real path to the file or directory on your home page, for example, if your Perl program is stored under C:/apache/cgi-bin, then C:/apache/cgi-bin is the absolute path to the Cgi-bin directory.

In the network, the beginning of the HTTP link is an absolute path, the absolute path is your home page file or directory on the hard disk real path, the absolute path in the CGI program is often used in the path configuration, and in the production of web pages actually rarely used.

II. special symbols used in relative paths

The following are some of the special symbols that are used to establish the path, and the meaning they represent.

"./": Represents the current directory.

".. /": Represents the previous level of a directory.

Start with "/": Represents the root directory.

There are page1.html files and Site2 folders under the root directory under Site1 and image/image.jpg,site1. Site2 has page2.html and page2image.jpg picture files.

1, the file in the current directory

page2.html Access Page2image.jpg

or

2, the file in the previous level directory

Image.jpg under the page1.html access image

Image.jpg under the page2.html access image

3, the next level of the file directory

page1.html access to page2image.jpg under Site2 folders

4, the root directory notation, any page access image under the Image.jpg picture

Frequently used and should pay attention to the place

A, CSS in the picture path. Write CSS image path, URL is the location of the picture for the style file

--index.html

--Css/main.css

--Images/1.jpg

If: index.html references main.css, and main.css references the 1.jpg in the images directory:

Background:url (.. /images/1.jpg) * * * Right

Background:url (images/1.jpg) * * * * ERROR

B, JS in the picture address is relative to the call JS page relative position.

document.getElementById ("IMG1"). Style.backgroundimage

= "url (.. /images/login.jpg) ";

Iv. Advantages and disadvantages and places to use

1, the advantages of absolute path

A, if someone plagiarized your website content, the link inside will also point to your website, some plagiarism person is lazy, do not change content at all. In fact, is not limited to plagiarism, if someone will save your web page to the local computer, the links, pictures, CSS, and JS will still be connected to your site.

B, if the page location changes, the link inside or point to the correct URL.

2, the absolute path of the shortcomings:

A, it is not convenient to use an absolute path when coding, because a link should point to a real domain name rather than a development site.

the relative path has the advantages and disadvantages and the absolute path is almost the opposite.

3, the advantages of the relative path:

A, easy to move content, you can move the entire directory.

B, the test method is more flexible, this machine test is more convenient.

4, the disadvantage of the relative path:

A, the partial content page changes the position, the link is apt to fail.

B, easy to be collected by the NPC area plagiarism.

V. Relative path and absolute path in system files similar to the one in the network, the path symbol for the file is a slash "/" and the network path is the opposite backslash "/"

Six, small Knowledge: "New Web Site" and "New virtual directory" difference, some people who used IIS will know that a new virtual directory, can be used to specify a directory to run the ASP program, but if the asp/html source code often appear like such a code "

Seven

(a), CS class to get the absolute path: System.Web.HttpContext.Current.Request.Url.Scheme + "://" + System.Web.HttpContext.Current.Request.Url.Authority + system.web.httpcontext.current.request.applicationpath+ "/ ";

(b) In a project, there are jslib/common.js files under the root directory. This is a common JS file, each page in the project will call this JS file a method. This approach is based on jquery's Ajax access to a method of returning a list, the path of Ajax access is the COMMON/MAIN.ASHX under the root directory. The question now is how to ensure that the path to this AJAX access is accessible in both the new Web site and the virtual directory.

For example, the Yhmge/useradd.aspx page in the root directory.

1, in the "virtual directory" to view the path (company is the virtual directory name):

Http://127.0.0.1/Company/YHMGE/UserAdd.aspx

2, in the new Web site to view the path:

Http://127.0.0.1/YHMGE/UserAdd.aspx

If you want to make sure you have access to both the new Web site and the virtual directory, you cannot write a common method for the above two connections to get the page based on several layers of the root directory. So there is a way to use absolute path, get common.js root path can access the handle page. We can get the absolute path of the/jslib/common.js file in the Common.js file, and then remove the "/jslib/common.js" section by this path, and then the common/main.ashx that will be visited will be able to connect to this path.

Get Root path (based on jquery):

function Getrootpath () {

var scriptobj = $ ("script[src$= '/jslib/common.js ')");

if (scriptobj==undefined)

{

Return "";

}

var srcbase = scriptobj.eq (0). attr ("src"). Replace ("/jslib/common.js", "");;

Return srcbase

}

1, in the "virtual directory" to view the path (company is the virtual directory name):

<script src= "Http://127.0.0.1/Company/JsLib/common.js" type= "Text/javascript" ></script>

After the interception is drawn: Http://127.0.0.1/Company/, and then connect "Common/main.ashx", HTTP://127.0.0.1/COMPANY/COMMON/MAIN.ASHX will be able to access the smooth

2, in the new Web site to view the path:

<script src= "Http://127.0.0.1/JsLib/common.js" type= "Text/javascript" ></script>

After the interception is drawn: HTTP://127.0.0.1/, and then connect "Common/main.ashx", HTTP://127.0.0.1/COMMON/MAIN.ASHX will be able to access the smooth

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.