231
votes

How can I get the title of an HTML page with JavaScript?

3
Note that there's a slight difference between using document.title and accessing the title element directly, see here. - Paul

3 Answers

370
votes

Use document.title:

console.log(document.title)
<title>Title test</title>

MDN Web Docs

17
votes

Put in the URL bar and then click enter:

javascript:alert(document.title);

You can select and copy the text from the alert depending on the website and the web browser you are using.

10
votes

Can use getElementsByTagName

var x = document.getElementsByTagName("title")[0];

alert(x.innerHTML)

// or

alert(x.textContent)

// or

document.querySelector('title')

Edits as suggested by Paul