Javascript Reference - HTML DOM Meta httpEquiv Property








The http-equiv attribute from meta element can be used to simulate an HTTP response header.

The httpEquiv property sets or gets an HTTP header for the information in the content attribute.

The value of the http-equiv attribute depends on the value of the content attribute.

Browser Support

httpEquiv Yes Yes Yes Yes Yes

Syntax

Return the httpEquiv property.

metaObject.httpEquiv 

Set the httpEquiv property.

metaObject.httpEquiv=HTTP-header

Some commonly used HTTP-header values are.

Value Description
content-type Specifies the character set for the contents of the document.

Example.

<meta http-equiv='content-type' content='text/html; charset=UTF-8'>

default-style Specifies the preferred style sheet to use.

Example.

<meta http-equiv='default-style' content='the documents preferred stylesheet'>

refresh Defines a time interval for the document to refresh itself.

Example.

<meta http-equiv='refresh' content='300'>





Return Value

A String type value representing information about the HTTP response message header.

Example

The following code shows how to change the value of the http-equiv and content attributes.

The following example will refresh the document every 30 seconds.


<!DOCTYPE html>
<html>
<body>
<!--from  w  w w  .j ava 2s  . c o m-->
<meta http-equiv="content-type" content="text/html">
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    document.getElementsByTagName("META")[0].httpEquiv = "refresh";
    document.getElementsByTagName("META")[0].content = "30";
    document.getElementById("demo").innerHTML = "The document will refresh every 30 seconds.";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the HTTP header for the information in the content attribute.


<!DOCTYPE html>
<html>
<body>
<!--from   w ww . java2 s  .  com-->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<button onclick="myFunction()">test</button>

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

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

</body>
</html>

The code above is rendered as follows: