简体   繁体   中英

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.

截屏

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM