Reputation: 14943
Is there any consideration that should be done between using the head or the body to add template tags? Ex:
conste template = document.createElement('template');
// add template content etc.
document.body.appendChild(template);
// or
document.head.appendChild(template);
I just stumble upon a code base that dynamically adds templates to head
and my gut tells me that maybe it isn't the best idea, but maybe it doesn't matter?
Upvotes: 7
Views: 1758
Reputation: 82976
Templates are among the most flexible of all the elements in where they can be placed. The spec says
Contexts in which this element can be used:
- Where metadata content is expected.
- Where phrasing content is expected.
- Where script-supporting elements are expected.
- As a child of a colgroup element that doesn’t have a span attribute.
"Where metadata content is expected." essentially means in the head.
"Where phrasing content is expected." essentially means anywhere the valid child of a body element can go.
"Where script-supporting elements are expected" means it can go even in places that phrasing content can't, such as the child of ul, ol, table, tbody, tr etc elements.
Upvotes: 7