What is links in javascript?

Example

Number of links in the document:

let numb = document.links.length;

Try it Yourself »

Get the URL of the first link in the document:

let url = document.links[0].href;

Try it Yourself »

Get the URL of the first link in the document:

let url = document.links.item(0).href;

Try it Yourself »

More examples below.


Definition and Usage

The links property returns a collection of all links in the document.

The links property returns an HTMLCollection.

The links property is read only.

The links in the collection represents <a> and <area> elements with an href attribute.


HTMLCollection

An HTMLCollection is an array-like collection (list) of HTML elements.

The elements in a collection can be accessed by index (starts at 0).

The length Property returns the number of elements in the collection.


Syntax

Properties

Property Description
length The number of elements in the collection.

Methods

Method Description
[index] Returns the element with the specified index (starts at 0).
Returns null if the index is out of range.
item(index) Returns the element with the specified index (starts at 0).
Returns null if the index is out of range.
namedItem(id) Returns the element with the specified id.
Returns null if the id does not exist.

Return Value

Type Description
Object An HTMLCollection Object.
All <a> and <area> elements in the document.
The elements are sorted as they appear in the document.


More Examples

Get the URL of the element with id="myLink":

let url = document.links.namedItem("myLink").href;

Try it Yourself »

Add a red border to the first link in the document:

document.links[0].style.border = "5px solid red";

Try it Yourself »

Loop over all links and output the URL (href) of each:

const links = document.links;
let text = "";
for (let i = 0; i < links.length; i++) {
  text += links[i].href + "<br>";
}

Try it Yourself »


Browser Support

document.links is a DOM Level 1 (1998) feature.

It is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes 9-11 Yes Yes Yes Yes


Links are found in nearly all web pages. Links allow users to click their way from page to page.


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!


The HTML <a> tag defines a hyperlink. It has the following syntax:

<a href="url">link text</a>

The most important attribute of the <a> element is the href attribute, which indicates the link's destination.

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:

<a href="https://www.w3schools.com/">Visit W3Schools.com!</a>

Try it Yourself »

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!


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:

<a href="https://www.w3schools.com/" target="_blank">Visit W3Schools!</a>

Try it Yourself »


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

<h2>Absolute URLs</h2>
<p><a href="https://www.w3.org/">W3C</a></p>
<p><a href="https://www.google.com/">Google</a></p>

<h2>Relative URLs</h2>
<p><a href="html_images.asp">HTML Images</a></p>
<p><a href="/css/default.asp">CSS Tutorial</a></p>

Try it Yourself »



To use an image as a link, just put the <img> tag inside the <a> tag:

Example

<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;">
</a>

Try it Yourself »


Use mailto: inside the href attribute to create a link that opens the user's email program (to let them send a new email):


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

<button onclick="document.location='default.asp'">HTML Tutorial</button>

Try it Yourself »


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.

Example

<a href="https://www.w3schools.com/html/" title="Go to W3Schools HTML section">Visit our HTML Tutorial</a>

Try it Yourself »


More on Absolute URLs and Relative URLs

Example

Use a full URL to link to a web page: 

<a href="https://www.w3schools.com/html/default.asp">HTML tutorial</a>

Try it Yourself »

Example

Link to a page located in the html folder on the current web site: 

<a href="/html/default.asp">HTML tutorial</a>

Try it Yourself »

Example

Link to a page located in the same folder as the current page: 

<a href="default.asp">HTML tutorial</a>

Try it Yourself »

You can read more about file paths in the chapter HTML File Paths.


Chapter Summary

  • Use the <a> element to define a link
  • Use the href attribute to define the link address
  • Use the target attribute to define where to open the linked document
  • Use the <img> element (inside <a>) to use an image as a link
  • Use the mailto: scheme inside the href attribute to create a link that opens the user's email program

TagDescription
<a> Defines a hyperlink

For a complete list of all available HTML tags, visit our HTML Tag Reference.



Websites use hyperlinks as a way to navigate online content. Hyperlinks can point to web content that is part of that website, or it can point to web content that is part of external websites. Both images and text can be used to create a hyperlink.
How to create a link in JavaScript ? // Create anchor element. var a = document..
Create an anchor <a> element..
Create a text node with some text which will display as a link..
Append the text node to the anchor <a> element..
Set the title and href property of the <a> element..
Append <a> element in the body..
Link types.
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!

Tải thêm tài liệu liên quan đến bài viết What is links in javascript?