tags:

views:

28

answers:

2

What is the difference between? Thank you.

<img src="images/file.jpg"></img>

between

<img src="/images/file.jpg"></img>

between

<img src="./images/file.jpg"></img>

between

<img src="../images/file.jpg"></img>
+2  A: 

You need to learn about relative and absolute paths.

Here are my explanations for your examples, but you realy should read the link in order to understand the concepts.

If the base URL is "http://example.com/resources/" then:

<img src="images/file.jpg"></img>

Will get:

http://example.com/resources/images/file.jpg

It simply adds the src url to the base URL.


<img src="/images/file.jpg"></img>

Will get:

http://example.com/images/file.jpg

Bacuse the image URL is rooted (starts with /) it uses the domain and adds the image src to the domain.


<img src="./images/file.jpg"></img>

Will get:

http://example.com/resource/images/file.jpg

In this case, it uses the relative path for the current directory (.), which is the base directory (resources).


<img src="../images/file.jpg"></img>

Will get:

http://example.com/images/file.jpg

In this case, it uses the relative path for the parent directory (..), which makes it go up a directory and then add the rest of the path.

Oded
so <img src="./images/file.jpg"> and <img src="images/file.jpg"> are functionally the same, right?
netrox
@netrox - pretty much. No real point to using `.`
Oded
A: 

The first, third and last ones are relative to the current path. In the last one, .. is the parent folder, which means you essentially ascend one level in the hierarchy, and in the second one . is the current folder, making the URI equivalent to the first one. The second one is relative to the root, since it starts with /. Read more about URIs in the HTML4 spec, or in general about Unix-style paths.

Thus, if you're at website.com/folder/folder/index.html, the four URIs would be equivalent to this:

website.com/folder/folder/images/file.jpg
website.com/images/file.jpg
website.com/folder/folder/images/file.jpg
website.com/folder/images/file.jpg
You
No, they're all relative. That is, the user agent will add information in order to create an absolute URI so it can request the resource. The difference is in what they're relative *to*. The first is relative to the current file (and is for all intents and purposes equivalent to the third, "./..."), the second relative to the server root, the third to the current directory and the fourth to the parent directory. A good argument can be made for root-relative links being a best practice for all local links, since it will not break if the host doc moves. An absolute URI includes the protocol.
Stan Rogers
Good point. However, I disagree with the statement that the last one is relative to its parent — it is relative to the current folder, except that the special folder `..` refers to the parent folder.
You