• 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
  • Ruby
  • 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.  innerHTML vs outerHTML
  • 2.  Using JavaScript to Get a Random Number Between 1 and 10
  • 3.  Using JavaScript to Get Date Format in dd mm yyyy
  • 4.  JavaScript Check If Number is a Whole Number
  • 5.  Using JavaScript to Return String
  • 6.  Using JavaScript to Replace Character in String
  • 7.  How to Convert Radians to Degrees Using JavaScript
  • 8.  Using JavaScript to Round to 2 Decimal Places
  • 9.  Using JavaScript to Get the Current Year
  • 10.  JavaScript Trig – How to Use Trigonometric Functions in Javascript

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy

x