Javascript Reference - HTML DOM Link href Property








The href property sets or gets the URL of a linked document.

Browser Support

href Yes Yes Yes Yes Yes

Syntax

Return the href property.

var v = linkObject.href 

Set the href property.

linkObject.href=URL

Property Values

Value Description
URL Specifies the URL of the linked document.




Return Value

A String type value representing the URL of the linked document.

Example

The following code shows how to Change style sheet.


<!DOCTYPE html>
<html>
<head>
<link id="myLink" rel="stylesheet" type="text/css" href="styles.css">
</head><!--from w w  w .  ja va  2 s. c om-->
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myLink").href = "style2.css";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the URL of the linked document.


<!DOCTYPE html>
<html>
<head>
<link id="myLink" rel="stylesheet" type="text/css" href="styles.css">
</head><!--from w  w w  .  jav  a 2  s  .  c  o  m-->
<body>
<button onclick="myFunction()">test</button>

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

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

</body>
</html>

The code above is rendered as follows: