- HTML Uniform Resource Locators
- URL — Uniform Resource Locator
- Common URL Schemes
- URL Encoding
- Try It Yourself
- ASCII Encoding Examples
- HTML Links
- HTML Links — Hyperlinks
- HTML Links — Syntax
- Example
- HTML Links — The target Attribute
- Example
- Absolute URLs vs. Relative URLs
- Example
- Absolute URLs
- Relative URLs
- HTML Links — Use an Image as a Link
- Example
- Link to an Email Address
- Example
- Button as a Link
- Example
- Link Titles
- HTML and URLs
- URLs as Addresses of Resources
- Query String
- Fragment Identifier
- Relative URLs
- The Advantage of Relative URL’s
HTML Uniform Resource Locators
A URL can be composed of words (e.g. w3schools.com), or an Internet Protocol (IP) address (e.g. 192.68.20.50).
Most people enter the name when surfing, because names are easier to remember than numbers.
URL — Uniform Resource Locator
Web browsers request pages from web servers by using a URL.
A Uniform Resource Locator (URL) is used to address a document (or other data) on the web.
A web address like https://www.w3schools.com/html/default.asp follows these syntax rules:
- scheme — defines the type of Internet service (most common is http or https)
- prefix — defines a domain prefix (default for http is www)
- domain — defines the Internet domain name (like w3schools.com)
- port — defines the port number at the host (default for http is 80)
- path — defines a path at the server (If omitted: the root directory of the site)
- filename — defines the name of a document or resource
Common URL Schemes
The table below lists some common schemes:
Scheme | Short for | Used for |
---|---|---|
http | HyperText Transfer Protocol | Common web pages. Not encrypted |
https | Secure HyperText Transfer Protocol | Secure web pages. Encrypted |
ftp | File Transfer Protocol | Downloading or uploading files |
file | A file on your computer |
URL Encoding
URLs can only be sent over the Internet using the ASCII character-set. If a URL contains characters outside the ASCII set, the URL has to be converted.
URL encoding converts non-ASCII characters into a format that can be transmitted over the Internet.
URL encoding replaces non-ASCII characters with a «%» followed by hexadecimal digits.
URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign, or %20.
Try It Yourself
If you click «Submit», the browser will URL encode the input before it is sent to the server.
A page at the server will display the received input.
Try some other input and click Submit again.
ASCII Encoding Examples
Your browser will encode input, according to the character-set used in your page.
The default character-set in HTML5 is UTF-8.
Character | From Windows-1252 | From UTF-8 |
---|---|---|
€ | %80 | %E2%82%AC |
£ | %A3 | %C2%A3 |
© | %A9 | %C2%A9 |
® | %AE | %C2%AE |
À | %C0 | %C3%80 |
Á | %C1 | %C3%81 |
 | %C2 | %C3%82 |
à | %C3 | %C3%83 |
Ä | %C4 | %C3%84 |
Å | %C5 | %C3%85 |
For a complete reference of all URL encodings, visit our URL Encoding Reference.
HTML Links
Links are found in nearly all web pages. Links allow users to click their way from page to page.
HTML Links — Hyperlinks
HTML links are hyperlinks.
You can click on a link and jump to another document.
When you move the mouse over a link, the mouse arrow will turn into a little hand.
Note: A link does not have to be text. A link can be an image or any other HTML element!
HTML Links — Syntax
The link text is the part that will be visible to the reader.
Clicking on the link text, will send the reader to the specified URL address.
Example
This example shows how to create a link to W3Schools.com:
By default, links will appear as follows in all browsers:
- An unvisited link is underlined and blue
- A visited link is underlined and purple
- An active link is underlined and red
Tip: Links can of course be styled with CSS, to get another look!
HTML Links — The target Attribute
By default, the linked page will be displayed in the current browser window. To change this, you must specify another target for the link.
The target attribute specifies where to open the linked document.
The target attribute can have one of the following values:
- _self — Default. Opens the document in the same window/tab as it was clicked
- _blank — Opens the document in a new window or tab
- _parent — Opens the document in the parent frame
- _top — Opens the document in the full body of the window
Example
Use target=»_blank» to open the linked document in a new browser window or tab:
Absolute URLs vs. Relative URLs
Both examples above are using an absolute URL (a full web address) in the href attribute.
A local link (a link to a page within the same website) is specified with a relative URL (without the «https://www» part):
Example
Absolute URLs
W3C
Relative URLs
HTML Images
CSS Tutorial
HTML Links — Use an Image as a Link
To use an image as a link, just put the tag inside the tag:
Example
Link to an Email Address
Use mailto: inside the href attribute to create a link that opens the user’s email program (to let them send a new email):
Example
Button as a Link
To use an HTML button as a link, you have to add some JavaScript code.
JavaScript allows you to specify what happens at certain events, such as a click of a button:
Example
Tip: Learn more about JavaScript in our JavaScript Tutorial.
Link Titles
The title attribute specifies extra information about an element. The information is most often shown as a tooltip text when the mouse moves over the element.
HTML and URLs
Every HTML document which is accessible via the web, is located at some URL. The word URL is short for Uniform Resource Locator. The URL is the «address» on the web of the HTML document.
URLs as Addresses of Resources
In fact it is not just HTML documents that have URL’s. Any file accessible via the web has a URL, including images, JavaScripts, CSS style sheet files, Flash files etc. All these files are called resources. Even dynamically generated files of data have a URL.
When you want to view a HTML document in a browser, you type in the URL of the document into the browsers address bar.
http://www.jenkov.com/books/jquery/index.html
This URL consists of three parts:
All three parts are illustrated here:
A URL consists of a protocol name, domain name, and resource path. |
The protocol tells what protocol is needed to access the resource the URL points to. Typically the protocol is either http or https .
The domain is a name that is translated into an IP address. Thus, the domain name really points to a server somewhere on the internet. This is the server hosting the resource. In the example above the domain is www.jenkov.com .
The resource path is the location of the resource within the server the resource is hosted on. In the example above, the resource path is /books/jquery/index.html . The resource path can be thought of as a logical directory structure. In the example above, the resource path contains two logical directories: books and jquery .
Query String
A URL can contain a query string. Here is an example:
http://www.jenkov.com/books/jquery/index.html?param1=value1¶m2=value2
The query string part of the above URL is:
The query string starts with a ? character, and then comes one or more name=value pairs. Each pair is separated by an & character. In the example above the query string contains two name=value pairs. The param1=value1 and the param2=value2 .
The name of a name=value pair is the name of a parameter passed to the server where the resource is hosted. The value is the value of the parameter named by the name.
It is up to the server how it interprets the query string, and whether a query string is needed at all to access a given resource. If a resource does not need a query string in its URL, but you add one anyways, the server typically just ignores the query string.
Fragment Identifier
A URL can contain a fragment identifier. A fragment identifier points to (identifies) a fragment of the HTML document the URL points to. Fragments are typically only used in HTML documents. Using a fragment identifier in the URL you can point not only to the HTML document itself, but to a location inside the HTML document. This is covered in more detail in the text about links.
Here is an example of a URL with a fragment identifier:
http://www.jenkov.com/books/jquery/index.html#someFragmentId
The fragment identifier is appended after the # character. Thus, the fragment identifier in the example above is someFragmentId .
The fragment identifier has to point to a fragment ID in the target HTML document. How to insert that, is explained in the text on links.
A URL can consist of just the fragment identifier. Here is an example:
In that case the URL is interpreted as pointing to a fragment ID inside the same document as the URL is contained in.
In case a URL has a query string appended to it, the fragment identifier is appended after the query string. Here is an example URL with both query string and fragment identifier:
http://www.jenkov.com/books/jquery/index.html?param1=value1¶m2=value2#someFragmentId
Relative URLs
A URL can be relative. A relative URL consists of just the resource path itself.
A relative URL is interpreted relative to the URL of the HTML document that contains the URL. Thus, if the URL of the HTML document containing the URL is:
http://jenkov.com/books/jquery/index.html
then all relative URL’s inside that HTML document are intepreted as being relative to that URL.
A relative URL containing just a document name, e.g. html-book.html is interpreted as being located in the same logical directory as the /books/jquery/index.html page. That means in the logical directory /books/jquery . The full resource path will be interpreted as:
The protocol and domain name is also interpreted as being the same as the document containing the relative URL. Thus, the resource path /books/jquery/html-book.html is interpreted to be located at the URL:
http://jenkov.com/books/jquery/html-book.html.
You can use two dots ( .. ) to signal that the relative URL points one directory up from the resource path of the document containing the URL. Thus, this relative URL
found inside a HTML document at the resource path /books/jquery/index.html , will be interpreted as the resource path:
Notice how the directory jquery is cut off the resource path, before the rest of the relative URL is appended to it.
The full URL will be interpreted as:
http://jenkov.com/books/html4/index.html
You can use multiple dots ( .. ) separated by a slash character ( / ) if you want the relative URL to go up more than one logical directory. Thus, the relative URL
inside a document located at resource path /books/jquery/index.html , will be interpreted as:
Notice how both the books and jquery logical directory path is cut off the documents URL, before the rest of the relative URL is appended to it.
The full URL will be interpreted as:
http://jenkov.com/html-book.html
A relative URL that starts with the slash ( / ) character is always interpreted as being relative to the root of the logical directory hierarchy, instead of to the URL of the document containing it.
Here is an example of a URL that is relative to the root of the logical directory structure:
It doesn’t matter what the URL is of the HTML document containing this URL, it will always be interpreted the same — as relative to the root of the logical directory hierarchy.
The Advantage of Relative URL’s
It can be an advantage to link internally between pages in your website using relative URL’s instead of full URL’s including protocol and domain name.