Saturday, October 25, 2025
HomeLanguagesJavascriptHow to get the information from a meta tag using JavaScript ?

How to get the information from a meta tag using JavaScript ?

To display meta tag information in HTML with JavaScript, we will use a method called getElementByTagName() function. 

Method 1: Using getElementsByTagName() method. 

Syntax:

document.getElementsByTagName("meta");

With this, we can get all the meta elements from an HTML file. As we click on the button, all the meta tag names and content will be displayed on the web page. 

Example 1: 

html




<body>
    <meta name="description" content="neveropen Article">
    <meta name="keywords" content="neveropen,GfG,Article">
    <meta name="author" content="Aakash Pawar">
    <button onclick="GfGFunction()">
        Click Here!
    </button>
    <br>
    <div id="demo">
        <h2>Content of Meta Tag</h2>
    </div>
    <script>
        function GfGFunction() {
        var meta = document.getElementsByTagName("meta");
        for (var i = 0; i < 3; i++) {
            document.getElementById("demo").innerHTML +=
            "name: <b>"+meta[i].name+"</b> and content: <b>"
            +meta[i].content+"</b><br>";
        }
        }
    </script>
</body>


Output:

How to get the information from a meta tag using JavaScript?

How to get the information from a meta tag using JavaScript?

 Method 2: Using getElementsByTagName() method with index specification. 

Syntax:

var meta = document.getElementsByTagName("meta")[0];

Here, the index number ‘0’ represents the first meta element. You can consider all the meta element in an HTML file as an array and can access them by specifying the index of the meta tag. This way, we can get specific meta elements from an HTML file. 

Example 2: 

html




<body>
    <meta id="author" name="description" content="neveropen Article">
    <meta id="author" name="keywords" content="neveropen,GfG,Article">
    <meta id="author" name="author" content="Aakash Pawar">
    <h1 style="color:green">neveropen</h1>
    <button onclick="GfGFunction()">Click Here!</button>
    <br>
    <div id="demo">
        <h2>Content of Meta Tag</h2>
    </div>
    <script>
        function GfGFunction() {
        var meta = document.getElementsByTagName("meta")[0];
        document.getElementById("demo").innerHTML +=
            "name: <b>"+meta.name+"</b> and content: <b>"
        +meta.content+"</b><br>";
        }
    </script>
</body>


Output: 

How to get the information from a meta tag using JavaScript?

How to get the information from a meta tag using JavaScript?

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS