• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / JavaScript / Using JavaScript to Get the Page Title

Using JavaScript to Get the Page Title

June 7, 2022 Leave a Comment

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 <title>Some title</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.

document.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:

<div id="div1">
  <p>The page title is:</p>
  <div id="page-title"></div>
</div>

<script>

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

</script>

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

Other Articles You'll Also Like:

  • 1.  Add Years to a Date Using JavaScript
  • 2.  Using JavaScript to Resize an Image
  • 3.  Add Minutes to a Date Using JavaScript
  • 4.  How to Clear a Textarea Field In JavaScript
  • 5.  Using JavaScript to Replace a Space with an Underscore
  • 6.  Using JavaScript to Wait 5 Seconds Before Executing Code
  • 7.  charAt() JavaScript – Getting the Index Position of a Character
  • 8.  How to Clear an Input Field in JavaScript
  • 9.  Using JavaScript to Get the Domain From URL
  • 10.  JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy