How to dynamically change the title of web page using JavaScript ?

Given a web page containing the page title and the task is to change the title of a web page dynamically using JavaScript.

Method 1: Using document.title property: The document.title property is used to set or return the current title of the document. The title of the page can be changed by assigning the new title as a string to this property. This will change the title of the website to the preferred title.
Syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

newPageTitle = 'The title has changed!';
document.title = newPageTitle;

chevron_right


Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to dynamically change a web
        page’s title using JavaScript ?
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        How to dynamically change a web
        page’s title using JavaScript ?
    </b>
      
    <p>
        Click on the button to change the page
        title to: "The title has changed!"
    </p>
      
    <button onclick="changePageTitle()">
        Change Page Title
    </button>
      
    <script type="text/javascript">
        function changePageTitle() {
            newPageTitle = 'The title has changed!';
            document.title = newPageTitle;
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
    title-property-before
  • After clicking the button:
    title-property-after

Method 2: Using querySelector() Method: The document.querySelector() method is used to select elements in the document. The title element can be selected by specifying the title element in the selector as a parameter. This will return the current title element of the page.

The textContent property of an element returns the text content of a specific node. The title of the page can be changed by assigning the required new title as a string to the textContent property. This will change the title of the website to the preferred title.

Syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

newPageTitle = 'The title has changed!';
document.querySelector('title').textContent = newPageTitle;

chevron_right


Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to dynamically change a web
        page’s title using JavaScript ?
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        How to dynamically change a web
        page’s title using JavaScript ?
    </b>
      
    <p>
        Click on the button to change the page
        title to: "The title has changed!"
    </p>
      
    <button onclick="changePageTitle()">
        Change Page Title
    </button>
      
    <script type="text/javascript">
        function changePageTitle() {
            newPageTitle = 'The title has changed!';
            document.querySelector('title').textContent
                    = newPageTitle;
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
    title-selector-before
  • After clicking the button:
    title-selector-after


My Personal Notes arrow_drop_up

Maybe this silence is dangerous

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.