Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the title of HTML page with JavaScript?

People also ask

How do I get the page title in HTML?

The <title> tag defines the title of the document. The title must be text-only, and it is shown in the browser's title bar or in the page's tab. The <title> tag is required in HTML documents!

How do you find the title of a Web page?

If you have trouble finding the “<title>” in the sea of HTML, then use the Find function. Again, on Windows, you can select Ctrl + F and then type “title” to quickly find the Title. That's all there is to it. Now you can easily find the webpage Title for any page on your website.

How do you write a title in JavaScript?

Use the querySelector() Method to Change the Page Title in JavaScript. We can use the document. querySelector() method to pick elements in a document. The title element can be chosen by giving the title element a selector parameter and retrieving the page's main title element.

What is title in JavaScript?

The title property sets or returns the value of the title attribute of an element, nothing but providing extra information regarding the element. This is mostly used as a tooltip, which displays the text on mouse hovering. Javascript has provided document. title() to get the title.


Use document.title:

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

MDN Web Docs


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.


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