Javascript DOM HTML Link href Property get

Introduction

Return the URL of the linked document:

var x = document.getElementById("myLink").href;

Click the button to return the URL of the linked document.

View in separate window

<!DOCTYPE html>
<html>
<head>
<link id="myLink" rel="stylesheet" type="text/css" href="style.css">
</head>//from  w  w w  .j  a va2s .  c  om
<body>

<h1>I am formatted with a linked style sheet</h1>
<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 href property sets or gets the URL of a linked document.




PreviousNext

Related