I got to have a tag inside a table because PHP writes a code there that adds stuff to an earlier created Javascript array. However, I get a validation error (4.01 strict).
Is there any way I can do this or is it simply forbidden to keep a script like this:
<TABLE> <TR> <SCRIPT></SCRIPT> <TD> </TD> </TR> </TABLE>
(is this better maybe?):
<TABLE> <TR> <TD> <SCRIPT></SCRIPT> </TD> </TR> </TABLE>
Change doctype? What do you think?
JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.
You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags. The <script> tag alerts the browser program to start interpreting all the text between these tags as a script.
You can add <script></script> inside a DIV tag. Just check on w3c, it is valid HTML.
SCRIPT
is not allowed in TR
as the content model of TR
is defined as (TH|TD)+
:
<!ELEMENT TR - O (TH|TD)+ -- table row -->
That means one or more elements of TH
or TD
.
But SCRIPT
is allowed in TD
. See the definition of TD
:
<!ELEMENT (TH|TD) - O (%flow;)* -- table header cell, table data cell-->
Where the parameter entity flow is defined as:
<!ENTITY % flow "%block; | %inline;">
And inline is defined as:
<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
And special is define as:
<!ENTITY % special "A | IMG | OBJECT | BR | SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO">
Ever since the release of HTML 5, it is legal to have a <script>
inside most kinds of table element, including tr
s.
At https://html.spec.whatwg.org/multipage/tables.html, you can see that the content models for table
, caption
, tbody
, thead
, tfoot
, tr
, td
and th
all include either "Flow content" or "one or more script-supporting elements". At https://html.spec.whatwg.org/multipage/dom.html#flow-content-2 and https://html.spec.whatwg.org/multipage/dom.html#script-supporting-elements-2 respectively, we see that script
s are defined to be both flow content and script-supporting elements, and are therefore allowed within any of those elements.
The notable exceptions are colgroup
s (which can only contain col
s and template
s) and col
s (which can't have content). You can't put a script
inside either of those.
This means that the asker's example HTML with a script inside a tr
...
<TABLE> <TR> <SCRIPT></SCRIPT> <TD> </TD> </TR> </TABLE>
... is now valid, which you can confirm at https://html5.validator.nu.
Gumbo's answer, stating that script
s are allowed inside td
s but not tr
s, was correct for HTML 4, but is now outdated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With