Javascript DOM HTML Link Object create

Introduction

We can create a <link> element via the document.createElement() method:

var x = document.createElement("LINK");

Click the button to create a LINK element.

View in separate window

<!DOCTYPE html>
<html>
<head>
</head>/*from w  w  w .ja  v a 2s .  co m*/
<body>
<button onclick="myFunction()">Test</button>
<h1>Header</h1>
<script>
function myFunction() {
  var x = document.createElement("LINK");
  x.setAttribute("rel", "stylesheet");
  x.setAttribute("type", "text/css");
  x.setAttribute("href", "style.css");
  document.head.appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related