0

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

Paul
  • 6,061
  • 6
  • 39
  • 70
  • `document.getElementsByTagName('title')[0].innerHTML`? I don't know *why* this works, but it does... – Nathan Chu Dec 10 '20 at 19:35
  • @nthnchu `document.getElementsByTagName('title')[0].innerHTML` is just [awful code](https://stackoverflow.com/questions/54952088/how-to-modify-style-to-html-elements-styled-externally-with-css-using-js/54952474#54952474). And don't use `.innerHTML` when the string doesn't contain any HTML. – Scott Marcus Dec 10 '20 at 19:36
  • @nthnchu just found it myself here, thanks: https://stackoverflow.com/a/43606154/2604492 – Paul Dec 10 '20 at 19:37
  • @Paul No. See my comment to your answer. – Scott Marcus Dec 10 '20 at 19:37
  • @SᴀᴍOnᴇᴌᴀ how is it a duplicate if the top answer suggests the code that caused my problem? – Paul Jun 08 '21 at 16:12
  • @Paul - after reading [this meta answer](https://meta.stackoverflow.com/a/321337/1575353) I have decided to retract my CV. I was thinking that even though the accepted answer doesn't answer your question the answer which you helped update satisfied it but others looking at this question might realize [that answer by SuperNova is what would be needed](https://stackoverflow.com/questions/1057059/how-to-get-the-title-of-html-page-with-javascript/43606154#43606154). – Sᴀᴍ Onᴇᴌᴀ Jun 08 '21 at 16:59
  • Well, he doesn't explain what's the problem and why his way is better. I just fixed obvious code problems in his answer. Nothing in the other question or any of the answers mention spacing. – Paul Jun 08 '21 at 17:15

1 Answers1

1

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

Paul
  • 6,061
  • 6
  • 39
  • 70
  • 1
    No. Use `document.querySelector("title").textContent` Your answer relies on just [horrible code](https://stackoverflow.com/questions/54952088/how-to-modify-style-to-html-elements-styled-externally-with-css-using-js/54952474#54952474). – Scott Marcus Dec 10 '20 at 19:37
  • 3
    I agree that `document.querySelector` is the more efficient way, but labeling the other as "horrible/awful code" is a bit dramatic. It's fine; just maybe not the "best." – daddygames Dec 10 '20 at 19:43
  • Thanks @ScottMarcus, I had no idea about "live" lists in JS. Good think that I asked the question. – Paul Dec 10 '20 at 19:45
  • A live list is not inherently bad, it's only implemented poorly in current systems. – Teemu Dec 10 '20 at 19:48
  • 1
    @Teemu It's not inherently good either. And, finding everything and putting it all in a collection, only to grab the first one and then throw away the collection is a waste of resources in all use cases. – Scott Marcus Dec 10 '20 at 19:53
  • @daddygames It is horrible. Scanning the entire DOM to find all matches, creating a live collection to store them in, selecting the first match and then discarding everything else that was found is horrible. So, it's a combination of bad techniques that create a solution that is worse than the sum of its parts. – Scott Marcus Dec 10 '20 at 19:55
  • 1
    It's unnecessary to insult the code. It works. it does the job. If the purpose of the code is simple and/or the performance doesn't matter, then it really doesn't matter if the other code is used. I think most people would recommend NOT to use that code simply because we should train ourselves to always choose the most efficient code. But like I said before: you're being dramatic calling it "horrible" or "awful." – daddygames Dec 10 '20 at 20:03
  • @daddygames *It works. it does the job. If the purpose of the code is simple and/or the performance doesn't matter, then it really doesn't matter if the other code is used.* <-- Yeah, sorry but statements like these are the reason why the same bad coding patterns continue to propagate. If your job is to write code and you tell your boss during a code review, "it works", you're in for a talking to. – Scott Marcus Dec 10 '20 at 20:07
  • @daddygames And, understanding that the #1 rule in software development is knowing that requirements ***will*** change in the future, means that starting off with simple code that gets the job done, but using bad code, ensures that this bad code will be migrated to more complex problems down the line. – Scott Marcus Dec 10 '20 at 20:07
  • 1
    @daddygames Lastly, I speak from 30 years of experience as a developer and a corporate trainer. I've seen what happens when this exact code is used. Very real performance problems manifest almost immediately when anti-patterns like using a live node list in a loop happen. Why use such bad code in the first place when the entire problem can be avoided with APIs and techniques that aren't 25+ years old? – Scott Marcus Dec 10 '20 at 20:10
  • 2
    @daddygames I'm not violating any code of conduct because I'm not insulting anyone. I'm point out that code is bad - - that's not a bad thing. In fact, that's exactly the kind of thing we are supposed to do here. The code I'm talking about is potentially dangerous to use. It warrants a severe response. Right up there with `eval()`. – Scott Marcus Dec 10 '20 at 20:11
  • 1
    @ScottMarcus Maybe we shouldn't hide the ugly truth of the live collections. A live collection traverses the entire DOM tree every time a value is read from it. I'd recall It was you Scott, who taught me this for some years ago, thank you for that!. – Teemu Dec 10 '20 at 20:33
  • @Auspex Bad code is bad, there's no need to sugar coat that and there's also nothing about that statement that translates to a person. There's no belittling or insulting about it. Have you ever sat through a code review? – Scott Marcus Jun 07 '21 at 20:36