How to get title of current HTML page using jQuery ?

Suppose you have given an HTML page and the task is to get the title of an HTML page with the help of only jQuery. There are two approaches that are discussed below:

Approach 1: The $(‘title’) jQuery selector is used to select the title element of the document and we can use text() method on the selector to get the title of the document.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML> 
    <html
      
    <head
        <title
            Get title of current HTML page
        </title>
        <script src
        </script>
        <style>
            body {
                text-align:center;
            }
            #gfg{
                color: green; 
                font-size: 24px; 
                font-weight: bold;
                  
            }
        </style>
    </head
      
    <body
        <h1 style="color:green;"
            GeeksforGeeks 
        </h1
        <b>
            Click on button to get HTML document's title
        </b
        <br><br>
        <button onClick="GFG_Fun()"> click here</button
        <p id="gfg"
        </p
          
        <script
            var down = document.getElementById('gfg'); 
          
            // Main function
            function GFG_Fun() { 
                down.innerHTML = $('title').text();
            
        </script
    </body
    </html>                    

    chevron_right

    
    

  • Output:

Approach 2: The $(document) jQuery selector is used to select the HTML document and we can use attr() method on the selector to get the title of the document by passing ‘title’ as argument.



  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML> 
    <html
      
    <head
        <title
            Get title of current HTML page
        </title>
        <script src
        </script>
        <style>
            body {
                text-align:center;
            }
            #gfg{
                color: green; 
                font-size: 24px; 
                font-weight: bold;
                  
            }
        </style>
    </head
      
    <body
        <h1 style="color:green;"
            GeeksforGeeks 
        </h1
        <b>
            Click on button to get HTML document's title
        </b
        <br><br>
        <button onClick="GFG_Fun()"> click here</button
        <p id="gfg"
        </p
          
        <script
            var down = document.getElementById('gfg'); 
          
            // Main function
            function GFG_Fun() { 
                down.innerHTML = $(document).attr('title');
            
        </script
    </body
    </html>                    

    chevron_right

    
    

  • Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.