Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Placing HTML markup inside script elements

2 views
Skip to first unread message

RobG

unread,
Sep 9, 2009, 11:38:27 PM9/9/09
to
It has been suggested that markup such as:


<script type="text/html" id="item_tmpl">
<div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>">
<div class="grid_1 alpha right">
<img class="righted" src="<%=profile_image_url%>"/>
</div>
<div class="grid_6 omega contents">
<p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text
%></p>
</div>
</div>
</script>

is Ok to use to create a template within an HTML document. The W3C
validator reports lots of errors if the DOCTYPE is HTML 4.01 strict.

Is the above markup OK?


--
Rob

Harlan Messinger

unread,
Sep 9, 2009, 11:54:33 PM9/9/09
to

It is not OK. The purpose of script tags is to put script in between
them. Why are you proposing to put script tags around HTML, and what
effect did you expect them to have?

C A Upsdell

unread,
Sep 9, 2009, 11:57:13 PM9/9/09
to

No. The SCRIPT tag has to enclose code in a scripting language
supported by the browser, usually JavaScript. Your SCRIPT doesn't.

RobG

unread,
Sep 10, 2009, 12:04:59 AM9/10/09
to
On Sep 10, 1:54 pm, Harlan Messinger

The idea is to put a template in the document, then use the script
element's innerHTML property to retrieve the template. The type of
"text/html" is intended to cause the browser to ignore the content.


--
Rob

Andy Dingley

unread,
Sep 10, 2009, 5:50:13 AM9/10/09
to
On 10 Sep, 04:38, RobG <robg...@gmail.com> wrote:
> It has been suggested that markup such as:
>
> <script type="text/html" id="item_tmpl">
>   <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>">
[...]

>   </div>
> </script>
>
> is Ok to use to create a template within an HTML document.

It isn't - at least not within the <script> element (you could do it
elsewhere, just hide it with CSS. I'd also suggest avoiding .innerHTML
in favour of DOM methods)

As to why, then refer to the HTML 4 recommendation: <SCRIPT> element
http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1

and particularly %Script;
http://www.w3.org/TR/html4/sgml/dtd.html#Script

<!ENTITY % Script "CDATA" -- script expression -->

explains why.


Your <div> requires a content model of %block; or %flow; to contain it
(which is most places in a HTML document) but the content model inside
of <script> is CDATA instead, "a sequence of characters from the
document character set and may include character entities." This
can't contain HTML markup (i.e. tags).


John Dunlop

unread,
Sep 10, 2009, 9:20:33 AM9/10/09
to
RobG:

> <script type="text/html" id="item_tmpl">
> <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>">
> <div class="grid_1 alpha right">
> <img class="righted" src="<%=profile_image_url%>"/>
> </

I would add to what others have already said that the first occurrence of
"</" in SCRIPT terminates its content. As the specification says, this is
the "</" of the SCRIPT end-tag in valid documents. Everything that follows
is outwith the SCRIPT element.

--
John

Scott Bryce

unread,
Sep 10, 2009, 10:10:39 AM9/10/09
to


I'm not sure what you are trying to accomplish, but I can't help but
think this would be better done on the server.

Andy Dingley

unread,
Sep 18, 2009, 5:22:02 AM9/18/09
to
On 10 Sep, 10:50, Andy Dingley <ding...@codesmiths.com> wrote:

> As to why, then refer to the HTML 4 recommendation:

There's a specific note too:
http://www.w3.org/TR/html4/types.html#h-6.14

"Script data ( %Script; in the DTD) can be the content of the
SCRIPT element and the value of intrinsic event attributes. User
agents must not evaluate script data as HTML markup but instead must
pass it on as data to a script engine."

Likewise for styles.

This would suggest that a user agent's DOM implementation shouldn't
populate itself from any HTML content found within <script> There's
also an infamous IE "issue" where it does exactly this (and thus makes
the page vanish), when fed XHTML with a en empty <script src="..." />
rather than separate start and end tags. The rest of the page is
parsed HTML-style as being within the <script> element, thus gets
ignored.

0 new messages