Javascript Reference - HTML DOM Meta Object








The Meta object represents an HTML <meta> element.

Meta Object Properties

Property Description
content Sets or gets the content attribute of a meta element
httpEquiv Sets or gets an HTTP header for the information in the content attribute
name Sets or gets a name for the information in the content attribute
scheme Not supported in HTML5.
Sets or gets how the content attribute should be interpreted

Standard Properties and Events

The Meta object supports the standard properties and events.





Example

We can access a <meta> element by using getElementsByTagName().


<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Web tutorials">
</head><!--  www .jav a  2 s  .  c om-->
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementsByTagName("META")[0].content;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

We can create a <meta> element by using the document.createElement() method.


<!DOCTYPE html>
<html>
<head>
</head><!--  w  ww  .j a va  2s .  c  o m-->
<body>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.createElement("META");
    x.setAttribute("name", "description");
    x.setAttribute("content", "Web tutorials");
    document.head.appendChild(x);

    document.getElementById("demo").innerHTML = "added";
}
</script>

</body>
</html>

The code above is rendered as follows: