Javascript Reference - HTML DOM Anchor rel Property








The rel attribute sets the relationship between the current document and the linked document.

The rel property sets or gets the value of the rel attribute of a link.

Browser Support

rel Yes Yes Yes Yes Yes

Syntax

Return the rel property:

var v = anchorObject.rel 

Set the rel property:

var v = anchorObject.rel='value';




Property Values

Value Description
alternate An alternate version of the document, for example, print page, translated version
author The author of the document
bookmark A related document
help A help document
licence Copyright information for the document
next The next document in a selection
nofollow nofollow tells that the Google search spider should not follow that link.
noreferrer Specifies that the browser should not send a HTTP referer header if the user follows the hyperlink
prefetch The target document should be cached
prev The previous document in a selection
search A search tool for the document
tag A keyword for the current document




Return Value

A String representing the ref value.

Example

The following code shows how to get the value of the rel attribute of a link.


<!DOCTYPE html>
<html>
<body>
<!--from  ww  w  . j a  va 2 s  . c  o m-->
<p><a id="myAnchor" rel="nofollow" href="http://www.example.com/">example</a></p>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myAnchor").rel;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:

Example 2

The following code shows how to set the value of the rel attribute to "nofollow".


<!DOCTYPE html>
<html>
<body>
<!-- w  w  w  .  j  av  a2s.co m-->
<p><a id="myAnchor" href="http://www.example.com">example</a></p>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myAnchor").rel = "nofollow";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: