The href
attribute is a URL pointing another resource from current page. There are several schemas for the href
attribute in <a>
tag:
http://yourdomain.com/images/example.png
//yourdomain.com/images/example.png
/images/example.png
images/example.png
Suppose the URL of your current page is (notice the "s" in protocol part)
https://mydomain.com/documents/index.html
The above "partial forms will be automatically converted by your web browser to following results:
http://yourdomain.com/images/example.png
https://yourdomain.com/images/example.png
https://mydomain.com/images/example.png
https://mydomain.com/documents/images/example.png
Explanation:
- href starting with
http://
,https://
, or//
are fully qualified URLs. Browser will use the string as full URL directly. - href starting with
//
is protocol agnostic, i.e. the resources can be accessed by eitherhttp
orhttps
. So the browser refers to the protocol used by current page to complete the protocol part. - The browser uses protocol and domain to complete the URL because the href starts with
/
, meaning reference to the "root folder" on this web server. - This needs to be contrasted with 3rd form. The href is just a relative path, i.e. without prefixing
/
. The browser uses protocol, domain and path of current page (https://mydomain.com/documents/
) to complete the URL.
Read more on Stack Overflow and Wikipedia