The href
property sets or gets the URL of a linked document.
href |
Yes | Yes | Yes | Yes | Yes |
Return the href property.
var v = linkObject.href
Set the href property.
linkObject.href=URL
Value | Description |
---|---|
URL | Specifies the URL of the linked document. |
A String type value representing the URL of the linked document.
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:
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: