How to get the title of HTML page with JavaScript?

JavascriptHtmlDomTitle

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

Use document.title:

console.log(document.title)

<title>Title test</title>

MDN Web Docs

Solution 2 - Javascript

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.

Solution 3 - Javascript

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

Solution 4 - Javascript

this makes the h1 element the page title. this shows how capable can be javascript for small and big projects.

document.getElementById("text").innerText = document.title;

<title>hello world</title>
<h1 id="text"></h1>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZA.View Question on Stackoverflow
Solution 1 - JavascriptZA.View Answer on Stackoverflow
Solution 2 - JavascriptNo MercyView Answer on Stackoverflow
Solution 3 - JavascriptSuperNovaView Answer on Stackoverflow
Solution 4 - JavascriptMarcell KraszniView Answer on Stackoverflow