Javascript Reference - HTML DOM Head Object








The Head class represents an HTML <head> element.

Standard Properties and Events

The Head object supports the standard properties and events.

Example

The following code shows how to get a <head> element by using getElementsByTagName():


<!DOCTYPE html>
<html>
<head>
  <title>title</title>
</head><!--   w ww .j  av  a 2s .  c om-->
<body>
<p id="demo"></p>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    var x = document.getElementsByTagName('HEAD')[0];
   document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to create a <head> element by using the document.createElement() method.


<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--  w  w  w.  j av a  2 s.  c  o m-->
     var x = document.createElement('HEAD'); 
     document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: