JavaScript document.title to Read and Change a Browser Tab Title

Read the current document title with document.title and assign a string such as document.title = 'New title' to update it. In an ordinary HTML document, this updates the <title> text in <head> and the title used in the tab.

Read the current document title

Set the initial title in the HTML <title> element. Reading document.title trims and collapses ASCII whitespace. The visible <h1> in the page body is a separate element. HTML Standard · document.title

// HTML: <title>Product list</title>
console.log(document.title); // Product list

Change and restore the title

To signal unsaved edits, capture the base title once. Build the displayed title from that base whenever the editing state changes. This prevents duplicate prefixes and gives you the original value to restore.

const originalTitle = document.title;

function setEditing(isEditing) {
  document.title = isEditing ? "Editing - " + originalTitle : originalTitle;
}

setEditing(true);
setEditing(true);
console.log(document.title); // Editing - Product list
setEditing(false);
console.log(document.title); // Product list

The assignment affects the current document. On reload, the loaded HTML and the scripts that run then determine the title again. Keep a title that should persist across visits in the HTML or server template.

Try changing a tab title in a standalone page

Save the code below as document-title.html in UTF-8 and open it in a browser. Enter a new title and select the change button. The tab and the current-title line update together. The restore button puts back the title read on load.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Document title demo</title>
</head>
<body>
<label for="new-title">New document title</label>
<input id="new-title" value="New document title">
<button id="apply" type="button">Change title</button>
<button id="restore" type="button">Restore original title</button>
<p id="current" aria-live="polite"></p>
<p id="message" aria-live="polite"></p>
<script>
const originalTitle = document.title;
const input = document.querySelector('#new-title');
const current = document.querySelector('#current');
const message = document.querySelector('#message');

function showTitle() {
  current.textContent = `Current title: ${document.title}`;
}

document.querySelector('#apply').addEventListener('click', () => {
  const title = input.value.trim();
  if (!title) {
    message.textContent = "Enter a title that is not blank.";
    return;
  }
  document.title = title;
  message.textContent = '';
  showTitle();
});
document.querySelector('#restore').addEventListener('click', () => {
  document.title = originalTitle;
  input.value = originalTitle;
  message.textContent = '';
  showTitle();
});
showTitle();
</script>
</body>
</html>

Submitting only whitespace shows an input message and keeps the current title visible. Applying a valid title or restoring it clears the message. Reload the page to see the initial title again.