What is the head tag for in the html?

2

I'm reading a book that at one point says the following:

  "Now we need a place to put our templates - one way is to use an existing element in our HTML, preferably hidden. You can achieve this by using the <script> element within <head> ."

  • What is the <head> tag used for?
  • What's the difference between putting something in <head> and elsewhere?
  • Is there any specific behavior that only happens in <head> and can not be played using the rest of the page?

The author follows:

  

"It may seem strange at first - but it works very well."

  • Why would it be strange to use this tag for this purpose?
asked by anonymous 21.04.2017 / 05:46

1 answer

5
  

What is the <head> tag used for?

The head element represents a collection of metadata for the document. Metadata is, as its name says, data about the data. In short, you can tell the browser some details about the content of the page. For example, <meta charset="UTF-8"> you are specifying the character collection that the browser should use to parse your page. You can still specify the author, describe the content of the page, have settings for SEO, inform style sheets for the browser to use in page rendering, etc. All documentation, including all possible elements for the head tag, you'll find here / a> (1).

  

What's the difference between putting something in <head> and elsewhere?

By default, all elements within head are not displayed by the browser. Expected behavior, since the element is intended to define metadata, not page data. The templates that the author cites are from the Handlebars library, so they are not contents that will be rendered directly to the user, but rather, content that will be available for JavaScript to use in the future (when executed). I believe that the author only defines these templates within the head element because they are not rendered, but any content within the script element that does not have MIME Type of JavaScript are disregarded by the browser when rendering the page. Therefore, an element defined as below, within the element body , would work in the same way.

<body>
  <h1>Renderizado</h1>
  <script type="text/html">
    <h1>Não renderizado</h1>
  </script>
</body>

To make it even more specific, you can use type="text/template" :

<body>
  <h1>Renderizado</h1>
  <script type="text/template">
    Aqui está meu {{template}}.
  </script>
</body>
  

Is there any specific behavior that only happens in <head> and can not be played using the rest of the page?

In this case, no. When it's not about metadata, I particularly think it's more semantic to include in the body element, since the template is part of the document's content, it just does not display.

Element template

There is also the template element that is defined just for this end. Your content is ignored by the browser when rendering the page and can be accessed via JavaScript.

<h1>Renderizado</h1>

<template id="h1">
  <h1>Não renderizado</h1>
</template>

I think the ideal is to use this element instead of script . Including, web components usually use this tag. The framework Polymer I know you use. Also read about shadow gift .

References

  • The head element
  • The script element
  • 21.04.2017 / 06:37