0

This has been a burning question for me ever since and I think it's interesting enough to discuss it on the forums. As most will know, in websites we include anchor links, stylesheets, script files (javascript) and images. For anchor links we use the form <a href="..." />
For stylesheets we may use the form <link rel="stylesheet" type="text/css" href="..." />
For javascript we may use <script src="..." />
For images we use <img src="..." />

So, the question is this:

How do we know that what is in the link pointer (i.e. replacing the ... in each example) is a local file or a foreign entity?

To make it clear, lets say I create a local file named "ashish.com". Now, my purpose is to create a link so that anybody who clicks on it may download it. So, my code would be thus:

<a href="ashish.com">Download It</a>

But this makes it ambiguous. I could also be referring to a website named "ashish.com" So, how does the computer magically know which one I mean? Or does it even know this? What would happen in such a scenario?

4

1 回答 1

0

Absolute URIs are prefixed by an URI scheme name. For example, URI scheme name of http://example.com/hello.jpg is http, while the URI scheme name of ftp://example.com/world.jpg is ftp.

Relative URIs don't have a prefix. An URI such as example.com/hello.jpg would be qualified as a relative URI.

This means that when, on your page, you have:

<a href="example.com">Magic</a>

the absolute location will depend on the location of the actual page. If you're viewing the page locally, its URI would be similar to:

file:///C:/Sample/index.html

so the link will point to:

file:///C:/Sample/example.com

If the page is hosted on a website and viewed through the browser, its location may be similar to:

https://sample.example.com/index.html

so the link would point to:

https://sample.example.com/example.com

Remark about URI schemes in address bars:

A confusion is easily made with the behavior of the UI of most browsers when accessing a website. Indeed, in a browser, when you type google.com in the address bar, the browser opens Google website. In most cases, you won't even see http:// in the address bar. This behavior is done exclusively for the users in order for them to avoid typing the URI scheme every time they want to access a website. The automatic redirection to https:// on some websites (such as Google) makes things even easier for the users.

Remark about HTTP/HTTPS:

There is a specific case where absolute links don't contain URI schemes. A link such as:

//static.example.com/logo.png

will receive a special treatment from the major part of the browsers: the actual URI scheme of the page will be reused.

This is particularly useful, for example, when the same website can be accessed both with HTTP and HTTPS. In the first case, the browser will access:

http://static.example.com/logo.png

while in the second one, it will switch to the HTTPS version of the CDN:

https://static.example.com/logo.png
于 2013-11-07T18:59:44.773 回答