Paul
Paul

Reputation: 6851

Get raw title as defined in the HTML markup with JavaScript

If I have a title with multiple consecutive spaces, document.title returns a string with a single space for each such space combo.

Example:
<title>[ ]</title> - HTML
"[ ]" - document.title

See also image below.

Question - how to get the raw string as it's defined in the HTML document? This caused a bug in one of my scraping scripts where a title should match some other element.

screenshot

Upvotes: 0

Views: 937

Answers (1)

Paul
Paul

Reputation: 6851

Looks like I found the answer myself, using the following works:
document.getElementsByTagName('title')[0].textContent

According to the comment of Scott Marcus (this answer), it's better to use:
document.querySelector("title").textContent

Upvotes: 1

Related Questions