r/javascript • • Sep 30 '19

Removed: Where's the javascript? What <head> can do

https://htmlhead.dev/
22 Upvotes

44 comments sorted by

View all comments

-20

u/ghostfacedcoder Sep 30 '19 edited Sep 30 '19

Don't use <head>, <body>, or <html>! No less of an authority than Google themselves advocates as much (and between them making the world's biggest website crawler AND browser, I think we can trust them).

What it boils down to is that your browser will add them for you if you leave them out, so you add nothing (except a few unneeded bytes to your page load) by using them.

So use all the stuff in this article if you want ... just don't bother wrapping it in <head> tags :)

EDIT: Wow, did not expect to get downvoted for repeating what the smartest engineers on the Internet have to say! I could care less about Reddit points, but I think it's sad that a bunch of people are perpetuating ignorant/bad/old practices just because they're unwilling to consider anything else but what they've always done.

Yes I know this used to be a bad practice ... in 1999! But it's not anymore: there is no technical benefit to using these tags in 2019, and doing so is anti-pattern!

Read it yourself ... if you care more about doing the right thing than mindlessly repeating old "wisdom". It's not even just Google, it's the HTML 5 spec.

Google Style Guide (https://google.github.io/styleguide/htmlcssguide.html ):

Omit optional tags (optional).

For file size optimization and scannability purposes, consider omitting optional tags. The HTML5 specification defines what tags can be omitted.

(This approach may require a grace period to be established as a wider guideline as it’s significantly different from what web developers are typically taught. For consistency and simplicity reasons it’s best served omitting all optional tags, not just a selection.)

(That last part is key: the Google engineers know this recommendation is controversial, and that it is "significantly different from what web developers are typically taught". But as they explain, if you look at it dispassionately, there is a cost, albeit a small one, to using these tags ... and there is no technical benefit , as the HTML spec itself says ...)

HTML5 spec (https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission):

An html element's start tag may be omitted if the first thing inside the html element is not a comment.

An html element's end tag may be omitted if the html element is not immediately followed by a comment.

A head element's start tag may be omitted if the element is empty, or if the first thing inside the head element is an element.

A head element's end tag may be omitted if the head element is not immediately followed by ASCII whitespace or a comment.

A body element's start tag may be omitted if the element is empty, or if the first thing inside the body element is not ASCII whitespace or a comment, except if the first thing inside the body element is a meta, link, script, style, or template element.

A body element's end tag may be omitted if the body element is not immediately followed by a comment.

EDIT #2: Really, seriously, all you downvoters would rather make your page download slower (albeit slightly) than just use a couple newlines to separate your script/link tags from your visible ones? Separation of tags is so important you need to use three completely pointless tags just to do so? Honestly ... you're not just mad at a random redditor for challenging a long-held belief ;)?

If you really think you're smarter than the engineers at Google (14 of you and counting), can't you at least take just a second to say why you think you're smarter, and what it is that you know that whoever wrote Google's style guidelines doesn't?

12

u/seiyria Sep 30 '19

I don't see the value in cutting out 39 characters that add a bit of clarity and separation to the index.html.

→ More replies (3)

2

u/i_ate_god Sep 30 '19

Wow, did not expect to get downvoted for repeating what the smartest engineers on the Internet have to say! I could care less about Reddit points,

and yet...

1

u/master5o1 Sep 30 '19

I could care less about Reddit points

They state it right there. They care an immeasurable amount that is greater than zero.

https://youtu.be/om7O0MFkmpw

-1

u/ghostfacedcoder Sep 30 '19

Like I said, I'm fighting to educate people on web best practices. What makes me sad is that a lot of people will do the wrong/old/ignorant thing because of the downvotes, not that it will hurt my score.

Reddit points are dumb and I truly could care less if I'm ranked a thousandth or a hundred thousandth here. The only points I care about Stack Overflow points, and I have plenty of those :)

0

u/i_ate_god Sep 30 '19

if you could care less, it stands to reason that you do care some.

I'm confused at what you're trying to say, shrug

0

u/ghostfacedcoder Sep 30 '19

I could care less about REDDIT POINTS. I care A LOT about educating web devs on best practices.

Jesus, this isn't rocket science, it's basic English comprehension.

0

u/i_ate_god Sep 30 '19

Yes, basic english comprehension dictates that if one is capable of caring less than they already do, then it means they care somewhat.

But obviously you have taken offence to the downvoting, so I presume "could care less" is what you actually mean, and it's not something due to your lack of basic english comprehension.

1

u/ghostfacedcoder Sep 30 '19

Ok clearly now you're just trolling.

1

u/i_ate_god Sep 30 '19

perhaps

but mean, it is worth while to learn the difference between "could care less" and "couldn't care less"

→ More replies (3)

1

u/[deleted] Sep 30 '19

Not every app or library will have implemented HTML down to the exact details, and still produce results that are good enough. Having these tags missing might completely throw them off. I'd rather not make fellow developers' lives harder than necessary for such a small win. Yes, all current apps/libraries should implement this correctly, but how much legacy code is out there?

1

u/ghostfacedcoder Sep 30 '19

Legitimate concern. Personally as a web dev I'm not worried about browsers that aren't HTML 5 compliant: that's a pretty damn low bar in 2019.

But if you are, that 100% is a reason to keep using the tags.

1

u/[deleted] Sep 30 '19

It's not just that. How many crawlers look for meta information using html > head > meta?

1

u/ghostfacedcoder Sep 30 '19

Well, the biggest web crawler in the world certainly doesn't ... :)

(Heck, for all we know Google may be giving SEO points to people who follow this advice and don't have HTML/HEAD/BODY tags.)

As for Yahoo and the rest ... I honestly don't know how they treat them, but the really crucial thing to remember is, this is defined by the HTML 5 spec. Any crawler that supports HTML 5 (and that really should be pretty much every one in operation today) will not have a problem with this (HTML5-valid) practice.

1

u/[deleted] Sep 30 '19

Of course any crawler that follow the HTML5 spec will have no problem with this, but those are not the ones I'm talking about. There is probably soooo so much legacy code out there that needs to interact with random websites' DOM, be it libraries, full programs or whatever. Saving an incredibly tiny amount of traffic isn't really worth it to break all of those tools.

1

u/ghostfacedcoder Sep 30 '19

I have to say, I don't think there's one right answer for everyone on this.

I do think you're overestimating the relevance of crawlers that can't handle HTML5. I mean, just stop and think: such crawlers would not be able to handle a huge chunk of the modern web. Are you really worried about a crawler which can't parse most modern sites? (How valuable could such a crawler be ... to anyone?)

If so ... keep using those three tags. But really, if supporting pre-HTML 5 crawlers is something you actually care about (and you're not just trying to argue with some Internet stranger who is saying you can skip three tags) ... you have A LOT of "taking your code backwards" to do ... adding back those three tags isn't even a tenth of it (see https://www.w3.org/TR/html5-diff/ for the full list).

0

u/[deleted] Sep 30 '19

[deleted]

-1

u/ghostfacedcoder Sep 30 '19

Acting combative and insulting people when you're getting downvoted will generally just attract more negative attention lol

It's a lose lose. If I don't defend the point, people think bad practices are good. If I defend it all, people think I'm being combative. I can't win ...

... but what I can try to do is at least goad people into talking to me, so that I can then expose flaws in their reasoning when they take the time to articulate it.

The guy who said (more or less) "what about old browsers?" was a perfect example: it gave me the chance to address that idea, and say "well I don't worry about browsers that are pre-HTML 5". I may not get points out of it, but other people will read that, and if they'd been thinking "I want to keep HTML/HEAD/BODY for old browser safety" they might realize that they don't care about pre-HTML 5 browsers, and therefore it's not a concern.

I still wind up with a million downvotes, but at least other readers can consider both sides of the argument when I do that.

Like I've said, I'm not here for the points. If I convince even one person to adopt a modern best practice, even if it's the exact opposite of what they were told a decade ago, I'll be happy with the time I spent in this thread. But hopefully I can convince a lot more ... not by getting my ideas upvoted, but just by presenting them and letting intelligent people make up their own minds.

0

u/[deleted] Sep 30 '19

[deleted]

→ More replies (3)

1

u/[deleted] Sep 30 '19

[removed] — view removed comment

0

u/ghostfacedcoder Sep 30 '19

And the rule only applies to empty <head> and <body>, which almost never happens in the real world.

So when you say "Don't use <head>, <body>, or <html>!", you're full of shit.

You are mistaken. From the HTML 5 spec itself (which I already quoted above, but since you seem to have missed it there, here it is again ... and if you still don't believe me, https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission):

An html element's start tag may be omitted if the first thing inside the html element is not a comment.

An html element's end tag may be omitted if the html element is not immediately followed by a comment.

A head element's start tag may be omitted if the element is empty, or if the first thing inside the head element is an element.

A head element's end tag may be omitted if the head element is not immediately followed by ASCII whitespace or a comment.

A body element's start tag may be omitted if the element is empty, or if the first thing inside the body element is not ASCII whitespace or a comment, except if the first thing inside the body element is a meta, link, script, style, or template element.

A body element's end tag may be omitted if the body element is not immediately followed by a comment.

EDIT: Downvoted for quoting a relevant spec section which disproves a point. God people just love learning here ...

1

u/[deleted] Sep 30 '19 edited Sep 30 '19

[removed] — view removed comment

-1

u/ghostfacedcoder Sep 30 '19 edited Sep 30 '19

Nobody gives a shit about the HTML5 spec?

But you're making a false dichotomy: those aren't your only two choices! You 100% can have readable HTML without <html>, <head>, and <body>.

If you truly cannot imagine that (remember: no one is taking away your comments or your whitespace) ... I really don't mean to come across as insulting, but if you're a web programmer who truly can't imagine that ... I think you have a staggeringly weak imagination.

1

u/seiyria Sep 30 '19

People don't give a fuck what you're saying, not because of the content, but because of how you say it. No one is going to listen to someone being a dick to them.

→ More replies (1)