Javascript DOM HTML Link Object get

Introduction

The Link object represents an HTML <link> element.

We can access a <link> element via document.getElementById():

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

Click the button to get 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>/*www.j  ava  2s .c  o  m*/
<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>



PreviousNext

Related