Html中src、href的相对路径与绝对路径

What is a path? Why is this something developers should care about?

A path is simply the location of a file or directory within a file system,1 though it’s not exactly an address. A path is both the location and how to get there (if this definition seems complicated, hopefully a few examples (coming shortly!) will clear things up). The reason that web developers need to understand paths is because the web is about URLs (such as http://example.com/index.html), and URLs are primarily paths. This is evident in the URL syntax: scheme://domain:port/path?query_string#fragment_id.2

The domain part of the URL maps to the IP address of a remote computer. When accessing that remote computer using a given scheme (HTTP) and port (by default, 80), you are accessing a portion of the remote computer’s file system. The layout of this accessible portion of the file system corresponds to the path part of the URL.

For the remainder of this post, we’ll use the fictional “example.com” as an example. Example.com has the following directory structure on their web host:

/ (The web root)
	css
		styles.css
	files
		example.pdf
		index.html
	img
		footer.png
		header.png
	index.html

When a user navigates to http://example.com/index.html in their browser, they are accessing the above file system and the web server “serves” them index.html. If a user wanted to download the example.pdf, they would navigate to http://example.com/files/example.pdf.

Types of paths

There are two ways to specify a path: absolute or relative. Absolute paths are a bit simpler, so we’ll start with them.

ABSOLUTE PATHS

An absolute path gives the location of a file or directory in reference to a root directory. For example.com (as for all websites!), the root directory is the web root (or /). Let’s look at some examples.

Given example.com, we’d like /index.html to include a link to the styles.css file. Using absolute syntax, we’d add a link element to the head like this: <link rel="stylesheet" href="/css/styles.css">.

If you wanted to add the header.png image to /index.html using an absolute path, it would look like this: <img src="/img/header.png">.

Let’s look at a few more examples of absolute paths. Using /files/index.html, let’s create a link to the example.pdf: <a href="/files/example.pdf">. And we’ll add a link to the styles.css in the head<link rel="stylesheet" href="/css/styles.css">.

Note that all of the absolute paths start with /. Basically, you can always combine a domain (example.com) with an absolute path to get a fully qualified URL.

Speaking of fully qualified URLs, that’s the alternative way of writing an absolute path:<link rel="stylesheet" href="http://example.com/css/styles.css">. I don’t recommend it, though.

RELATIVE PATHS

A relative path is a path to a given file or directory starting from another file or directory. To make this simple, we’ll look at several examples.

Given example.com, we’d like /index.html to include a link to the styles.css file. Using relative syntax, we’d add a link element to the head like this: <link rel="stylesheet" href="css/styles.css">. This translates to “look in the css directory that is in the same directory as index.html and get the styles.css file from there.” Note: the difference between this relative path and the absolute one is the omission of the leading /.

If you wanted to add the header.png image to /index.html using a relative path, it could look like this: <img src="img/header.png">. It could also look like this: <img src="./img/header.png">. is a “special character” when used like this. It means “start at the current directory.”

Let’s look at a few more examples of relative paths. Using /files/index.html, let’s create a link to the example.pdf: <a href="example.pdf"> (or <a href="./example.pdf">). So far, so good. Now let’s add the styles.css in the head. Uh oh. So far, we’ve always looked in the current directory or down. How to go up?

.. is the “special character” to go up one directory. So, back to our example. To add styles.css in the head of /files/index.html, you’d use the path ../css/styles.css. To go up two directories, use ../../. Three directories: ../../../. Etc.

Which should I use? Relative or absolute? Or… does it matter?

Given the ability to link to files using a relative or absolute path or a fully qualified URL, does it matter which one we use?

For the end user, not really. I’ve seen some articles that make some strange claims about performance (such as local absolute paths go through DNS, but relative paths don’t!) andSEO benefits, but the only practical difference is a few bytes saved by using relative paths. I created a test page to demonstrate the lack of difference athttp://jeffreybarke.net/tests/paths/.

The primary reason to prefer one to another is for the benefit of the web developer! For instance, relative links make it very easy to move “chunks” of a site from one location on a web server to another without having the links break. As long as the chunks maintain their relative structure, they can be moved to any subdirectory at will.

The primary disadvantage to using relative links shows up when you start creating larger, more dynamic sites. Each subdirectory of the site requires a different relative path to get at common assets (such as style sheets or images). Since large, dynamic sites typically have a shared header file, it makes more sense to use absolute links.

Both relative and absolute paths make it easy to work on a site locally and then move the files to a remote web server. With relative paths, the local site structure doesn’t need to match the remote one; with absolute paths, it does. (You’ll also need to run a web server locally to use absolute paths, but that’s also a necessity to run a dynamic site locally.)

The previous paragraph hints at the reason why I don’t recommend using fully qualified URLs—without manipulating DNS entries (with hosts ,for example), it’s impossible to work locally. The local files will always point to the remote server!

References

  1. “Path (computing).” (n.d.). In Wikipedia. Retrieved 15 June 2013, fromhttp://en.wikipedia.org/wiki/Path_(computing)
  2. “Uniform resource locator.” (n.d.). In Wikipedia. Retrieved 15 June 2013, fromhttp://en.wikipedia.org/wiki/Uniform_resource_locator

原文网址:http://jeffreybarke.net/2013/06/paths-and-urls-relative-and-absolute/

时间: 2024-11-05 11:37:38

Html中src、href的相对路径与绝对路径的相关文章

PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能

PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能 网上很多正则表达式只能获取或者替换一个img的src内容,或者只能替换固定的字符串,要动态替换多个图片内容的试了几个小时才解决. /*** 图片地址替换成压缩URL* @param string $content 内容* @param string $suffix 后缀*/function get_img_thumb_url($content="",$suffix="!c550x260.jpg&q

web项目中,视图层中关于相对路径和绝对路径

1.在jfinal项目中 因为一直使用的jfinal,没感觉路径问题. 举个栗子,项目名字叫做test.访问一个Controller的映射为/user/add.这样,在浏览器地址栏直接:localhost:8080/user/add就可以直接访问到add方法了.当然,这样需要通过配置不同的端口来发布不同的项目,不然肯定冲突了.端口指定项目的路径. 比如: <Host name="localhost" appBase="webapps" unpackWARs=&

JSP、Servlet中的相对路径和绝对路径

1.JSP.Servlet中的相对路径和绝对路径  前提:假设你的Http地址为http://192.168.0.1/你的web应用为test,path="/test"那么你的web应用URL为http://192.168.0.1/test/ 如果JSP,JS文件放在WEB-INF目录下根本无法访问的,JSP如果放在WEB-INF目录下可以通过服务器内部转向进行访问(主要是为了页面的安全),但是JS是通过客户端向服务器请求的,所以图片以及一些JS,CSS只能放在WEB-INF外面  

编译过程中,termcap.h 文件找不到路径 licli.a终于生成

编译过程中,termcap.h      文件找不到路径 查看是linux  源码下找不到termcap.h文件 安装了所有关于*cap*的源码包也不起作用 今天终于解决了这个问题,搜termcap.h  发现一篇文章,如下 ----------------------------------------------------------------------------------------- 安装minicom2.3出现termcap.h错误解决方法 2010-05-06 17:12:

网站中图片的相对路径与绝对路径

1.相对路径 网站中加载图片所用到的相对路径,相对路径是以网页所在位置为参考的. ../代表上一级目录 src="../../photo/1.png"; src="images/1.jpg"; 2.绝对路径

[原创]java WEB学习笔记35:java WEB 中关于绝对路径 和相对路径问题

本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 ---------------------------------

JSP中&lt;base href=&quot;&lt;%=basePath%&gt;&quot;&gt;作用

通常在JSP页面开通有如下代码: 1 <% 2 String path = request.getContextPath(); 3 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 4 5 %> 这段代码的意思是获取当前项目的路径,如:http://localhost:808

vue中src下的assets文件与static文件的几点区别

区别一: assets文件时src下的,所以最后运行时需要进行打包:而static文件不需要打包就直接放在最终的文件中了. 区别二: assets中的文件在.vue中的template/style下用 ../ 这种相对路径的形式进行引用,在script下必须用 @import 的方式引入: 而 static 下的文件在.vue中的任何地方都只要使用 ../ 这种相对路径的方式引入.

img, script, link 的 src/href 为空时的bug

重复加载 这个 bug 并不新鲜.早在 2009 年,Nicholas C. Zakas 就发现了空 src 的危害性:Empty image src can destroy your site. Nicholas 的发现可以概括为一句话:img, script, link 的 src/href 为空时,有可能会导致冗余请求. 今天这个 bug 的起因,可以补充 Nicholas 的发现:CSS 里,background url 为空时,也有可能会导致冗余请求. 除了空值,还有一个值也会出问题:

网页制作中绝对路径和相对路径的区别

网页制作中绝对路径和相对路径的区别 http://www.veryhuo.com 烈火网 2012-10-10 投递稿件 做个开发的人,对于文件或者文件夹的定义都是有一定的规范的,比如建立一个网站前端设计,要建立的文件夹有css,js,images,swf等等,如图: 很多文件之下单个页面的连接,文件引用的时候就需要对路径这个问题有一定的了解:或者对php 网络程序开发有了解的程序员都会对define和include结合定义变量路径和引用的有很多见解. 比如: define('ROOT_PATH