Open In App

How to get the title of an HTML page ?

Last Updated : 20 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to get the title of HTML page using JavaScript. There is a number of ways to do this but here we discuss a few of the most preferred methods. 

Example 1: This example gets the title of the document by using document.title property

html




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        GeeksforGeeks Page Title
    </title>
</head>
 
<body style="text-align: center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
 
    <h3>
        Click on button to get the
        title of HTML document
    </h3>
     
    <button onClick="GFG_Fun()">
        Click Here
    </button>
     
    <p id="GFG2"></p>
     
    <script>
        let elm = document.getElementById('GFG2');
 
        function GFG_Fun() {
            elm.innerHTML = document.title;
        }
    </script>
</body>
 
</html>


Output:

 

Example 2: This example gets the title of document by selecting the title by tag name and then taking the element at index 0 using document.getElementsByTagName() method. 

html




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        GeeksforGeeks Page Title
    </title>
</head>
 
<body style="text-align: center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
 
    <h3>
        Click on button to get the
        title of HTML document
    </h3>
     
    <button onClick="GFG_Fun()">
        Click Here
    </button>
     
    <p id="GFG"></p>
     
    <script>
        let elm = document.getElementById('GFG');
 
        function GFG_Fun() {
            let title = document
                .getElementsByTagName("title")[0];
 
            elm.innerHTML = title.innerHTML;
        }
    </script>
</body>
 
</html>


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads