About absolute paths and relative paths

Source: Internet
Author: User
Tags httpcontext relative root directory

First, the basic concept

1, relative path-as the name implies, the relative path is relative to the current file path. Web pages typically represent paths using this method.

2. Absolute path-Absolute path is the real path on the hard disk of the file or directory on your home page. The absolute path is the real path on the hard drive of 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 link beginning with HTTP is an absolute path, the absolute path is the real path of the file or directory on your home page, and the absolute path is often used in the path configuration of the CGI program, which is seldom used in the production Web page.

II. special symbols used for relative paths

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

"./": Represents the directory where it is currently located.

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

Start with "/": Represents the root directory.

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

1. File in current directory

page2.html Access Page2image.jpg

or

2, the file in the previous level directory

page1.html access to image.jpg under image

page2.html access to image.jpg under image

3. Files in the next level directory

Page1.html accessing the page2image.jpg under the Site2 folder

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

Three, commonly used and should pay attention to the place

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

--index.html

--Css/main.css

--Images/1.jpg

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

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

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

B, JS in the image address relative to the call JS the relative position of the page.

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

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

Iv. advantages and disadvantages and place of use

1, the advantages of absolute path

A, if someone copied your site content, the link will also point to your site, some people are more lazy plagiarism, not to change the content. In fact, is not limited to being copied, if someone will save your page to the local computer, the links, pictures, CSS, and JS will still connect to your site.

B, if the page location changes, the link is still pointing to the correct URL.

2. The disadvantage of absolute path:

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

the pros and cons of relative paths are almost the opposite of absolute paths.

3, the advantages of relative path:

A, easy to move content, can be moved throughout the directory.

B, the test method is more flexible, the local test is more convenient.

4, the disadvantage of the relative path:

A, some content page changed position, the link is easy to fail.

B, easy to be large-scale collection of plagiarism.

V, relative path and absolute path in system files similar to in the network, the path symbol of the file is a slash "/", and the network path is the opposite of its backslash "/"

Six, small knowledge: The difference between "new Web Site" and "New virtual directory", some used IIS will know the new virtual directory, can be used to specify a directory to run the ASP program, but if the asp/html source code often appear similar to the code "

Seven

(a), the 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, the root directory has the Jslib/common.js file. This is a common JS file, each page in the project will call this JS file a method. This method is a method based on jquery's Ajax access to the return list, and the path to the Ajax access is common/main.ashx under the root directory. The problem now is how to ensure that the path to the 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. View the path in the virtual directory (company is the virtual directory name):

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

2. In the new Web site, view the path:

Http://127.0.0.1/YHMGE/UserAdd.aspx

If you want to ensure that both "new Web site" and "virtual directory" can be accessed, for the above two connections, unable to write a common method to get the page based on the root directory there are several layers. So one idea is to use the absolute path and get the root path of the common.js to 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, then the common/main.ashx to be accessed will be in this path.

Get the 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. View the path in the virtual directory (company is the virtual directory name):

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

Interception after the conclusion: Http://127.0.0.1/Company/, and then connected to the "common/main.ashx", to obtain HTTP://127.0.0.1/COMPANY/COMMON/MAIN.ASHX will be able to access the smooth

2. In the new Web site, view the path:

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

Interception after the conclusion: HTTP://127.0.0.1/, and then connected to the "common/main.ashx", to obtain 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.