user1566994
user1566994

Reputation: 304

Custom DTD in HTML 5

I'm experimenting with custom tag in html 5.

I tried the following:

<my-script src='script.js' />

this is inside the 'head' tag in the source code - but the browser (FF \ chrome) renders it inside the body. Also, it is rendered with an extra 'closing' tag:

<my-script src='script.js'> </my-script>

And, all the content of the 'body' tag gets nested inside this custom tag (the browser is wrapping the content of 'body' with my custom tag).

I tried using a custom DTD, but just can't get it to work...any ideas anyone ?

Upvotes: 2

Views: 505

Answers (1)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201896

Browsers treat a tag like <my-script src='script.js' /> as the start tag of an unknown element (except if the page is served with an XML content type). Since such a tag is not allowed within head, it implicitly closes the head element and starts the body element.

DTDs have nothing to do with this, since browsers don’t even read DTDs (and there is no DTD for HTML5, and it is impossible to write a DTD that matches HTML5 syntax rules).

So, in effect, you cannot use a custom element inside the head element. If you wish to use a custom element with no content, put it inside the body element and write it with the end tag spelled out: <my-script src='script.js'></my-script>. That way, it will not affect the display or the parsing of the page, and will have no effect except via client-side scripting that accesses it via the DOM.

Upvotes: 1

Related Questions