Forum Moderators: open

Message Too Old, No Replies

HTML comments inside script

         

Readie

5:07 pm on May 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is just idle curiosity on my part, but I've sometimes seen people have their JavaScript done in this fashion:

<script type="text/javascript">
<!--

//-->
</script>

Now, the only reason I can think of to do this - at all - is for legacy browsers that don't understand the <script> tag, interpreting the JavaScript as text.

Am I right, or is there another reason, or is it purely cosmetic?

mattur

6:13 pm on May 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for legacy browsers that don't understand the <script> tag, interpreting the JavaScript as text.


That's right. It's no longer necessary, hasn't been for years, and only persists due to our copy-paste web culture :-)

g1smd

8:00 pm on May 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't put JavaScript into an HTML file for years.

It always goes in an external JS file. :)

daveVk

11:20 pm on May 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What goes inside the script tags is not markup or correctly escaped text, without the comments the html or xhtml may not be valid.

Try validating your page.

Readie

12:38 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I try to avoid putting scripts directly into the page unless it's a trivially small script that is not going to be used anywhere else.

On the note of potentially invalid mark-up though... Even on pages where I have directly inserted the script, for both XHTML 1.0 and HTML 4.01 transitional doctypes it has passed the W3C validator test.

Havn't tested on a strict doctype however.

daveVk

1:10 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[webmasterworld.com...] re xhtml validation

Fotiman

2:14 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



HTML comments are NOT needed to validate XHTML pages that contain <script> elements with JavaScript on the page. As can be evidenced by pasting this code into the W3 Validator [validator.w3.org]:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>HTML Comments In SCRIPT are *NOT* needed</title>
</head>
<body>
<script type="text/javascript">
alert('this is in a script element with no html comments');
// It validates just fine, thank you very much!
</script>
</body>
</html>


As was pointed out in that thread, it's always best to put JavaScript in an external file anyway. If, however, you DO decide to include your JavaScript code within your HTML page, then the correct way to do so is not with HTML comments, but by placing the code within a CDATA section. This will allow your code to validate, even if you are using other bad practices like using document.write. ;)

<script type="text/javascript">
//<![CDATA[
document.write('<script>alert("this is in a script element with no html comments")<\/script>');
// It validates just fine, thank you very much!
//]]>
</script>


Short answer, though, is no, do not put HTML comments in your script tag. Hasn't been needed for many, many, many years.

mattur

4:25 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If, however, you DO decide to include your JavaScript code within your HTML page, then the correct way to do so is not with HTML comments, but by placing the code within a CDATA section.

In XHTML pages, yes. HTML pages do not require CDATA sections around script blocks. The reason your example CDATA tags begin with "//" is to comment out the CDATA bits when the document is parsed as HTML.

"In XHTML [w3.org] ... < and & will be treated as the start of markup, and entities such as &lt; and &amp; will be recognized as entity references by the XML processor to < and & respectively. Wrapping the content of the script or style element within a CDATA marked section avoids the expansion of these entities."

i.e. CDATA blocks are only required in XHTML documents when the script contains < or &.

If you're using XHTML as XML you can just use:
<script type="text/javascript"> 
<![CDATA[
...
]]>
</script>

If using CDATA blocks in pseudo-XHTML parsed as HTML you must use:
<script type="text/javascript"> 
//<![CDATA[
...
//]]>
</script>

or:
<script type="text/javascript"> 
/* <![CDATA[ */
...
/* ]]> */
</script>

To avoid this complexity, use HTML:
<script type="text/javascript">
...
</script>

In HTML5 documents, the type attribute is not required either:
<script>
...
</script>

Fotiman

5:18 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To avoid this complexity, use HTML:

Best of all, to avoid this complexity:

<script type="text/javascript" src="YOURFILE.JS"></script>

:)
And nice explanation mattur. :)

daveVk

2:55 am on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for advice on use of CDATA vs comments.

If it must work regardless of doctype assumably a safe option is

<script type="text/javascript">
//<![CDATA[
...
//]]>
</script>

mattur

10:17 am on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, for XHTML documents the standard way is:
<script type="text/javascript"> 
//<![CDATA[
...
//]]>
</script>

For HTML documents, no enclosing comments or CDATA tags are required:
<script type="text/javascript"> 
...
</script>

Or as Fotiman said, use an include file :)