In JavaScript, to get the page title of a post or article we can simply use the Document Object to do this. Almost all of the time, the title of an article or page will be in the title tag Some title. So to retrieve the title from the title tag, we simply need to get the title of the document object.

So here is the simple code to retrieve the title.


The title of this article is “Using JavaScript to Get the Page Title”. So we can use the code above to get the title and print it to the screen below.

In our simple JavaScript setup, we will simply get the title using document.title, and then display the text using the textContent property.

Here is the JavaScript code:

var pageTitle = document.title;
document.getElementById("page-title").textContent = pageTitle;

Here is the final code and output for this example of using JavaScript to get the page title.

Code Output:

The page title is:

Full Code:

The page title is:

Hopefully this article has been useful for you to understand how to use JavaScript to get the page title.

Categorized in:


Last Update: March 12, 2024