Complete Guide to Getting HTML Page Title with JavaScript

Nov 22, 2025 · Programming · 10 views · 7.8

Keywords: JavaScript | HTML | DOM | Page Title | document.title

Abstract: This article provides an in-depth exploration of how to retrieve HTML page titles using JavaScript, detailing the usage, working principles, and practical applications of the document.title property. Through comprehensive code examples and DOM operation principles analysis, it helps developers fully master the techniques for getting and setting page titles. The article covers basic usage, property characteristics, browser compatibility, and best practices, offering practical technical references for front-end development.

Basic Methods for Getting Page Title with JavaScript

In web development, retrieving HTML page titles is a common and important task. JavaScript provides a concise and powerful way to accomplish this, primarily through the document.title property.

Core Characteristics of document.title Property

The document.title property is a standard property of the Document interface, used to get or set the current document's title. This property returns a string value representing the document's title content.

When a page loads, document.title defaults to the content defined in the <title> element. For example, if the HTML document contains:

<title>Page Title Example</title>

Then executing through JavaScript:

console.log(document.title);

Will output in the console: Page Title Example.

Read-Write Operations of the Property

The document.title property supports both read and write operations, meaning you can not only get the title but also dynamically modify it. When setting a new title value, three places are updated simultaneously:

Example code demonstrates the complete read-write process:

// Get current title
var currentTitle = document.title;
console.log('Current title:', currentTitle);

// Set new title
document.title = 'New Page Title';

// Verify title has been updated
console.log('Updated title:', document.title);

Practical Application Scenarios

In actual development, document.title finds extensive applications:

Dynamic Title Updates

In single-page applications (SPA) or scenarios requiring dynamic page title updates based on user actions:

// Update title based on user action
function updatePageTitle(newTitle) {
    document.title = newTitle;
}

// Example: Update title after form submission
document.getElementById('submitBtn').addEventListener('click', function() {
    document.title = 'Form Submitted - ' + document.title;
});

Title Status Indicators

In scenarios requiring notification displays or status changes:

// Display unread message count
function showUnreadCount(count) {
    if (count > 0) {
        document.title = '(' + count + ') ' + document.originalTitle;
    } else {
        document.title = document.originalTitle;
    }
}

// Save original title
document.originalTitle = document.title;

Browser Compatibility and Standard Specifications

The document.title property has excellent browser compatibility and has been widely supported across all major browsers since July 2015. This property follows HTML standard specifications, ensuring cross-browser consistency.

According to HTML specifications, the behavior of document.title is defined as follows:

Best Practices and Considerations

When using document.title, it's recommended to follow these best practices:

By mastering the usage of the document.title property, developers can more flexibly control page titles, enhancing user experience and application interactivity.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.