[web] [js] Practices - Is using 'comment-masked' script tags useful anymore?

Started by
6 comments, last by markr 16 years, 11 months ago
Is there any practical use to surrounding the text within a <script> tag with comment identifiers [can't type them on here...] to hide it from 'old' browsers? Or have we been able to move on from there, where these 'old' browsers are essentially non-existant? I don't know why, but I still do this, and I want to see others' opinions on this...
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Advertisement
You really can't go wrong here anymore, fact of the matter is adding the code doesn't even amount to a kb of extra data being transmitted. Not taking the extra bytes will only affect a very discreet amount (or completely absent) of people.

The only thing to worry about when using &#106avascript is to not rely on it's uses nor force the user to have it turned on. (ex1: form validation ex2: shopping cart checkout requiring &#106avascript to create totals and such)<br><br>
Mark A. Drake
OnSlaught Games
Mark Drake
Thanks for the comment. I'm going to stop, since there doesn't seem to be that much of problem with it.
I don't know why it bothers me so much, but the HTML comments just seem to be a pain to type... Just my OCD-ness coming into play.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
For the future, if you ever develop a content management system you can create your own style of comments and have the template system remove comments before printing. You can also edit existing templates to remove these types of strings, of course this all might just be extra work but it's something I completed while learning how to work with strings.
Mark A. Drake
OnSlaught Games
Mark Drake
There is no point whatsoever.

The "comment-masked" script tags were only for browsers which were so old, they didn't understand script tags.

These days, even browsers which do not support &#106avascript, still understand script tags (i.e. that they should ignore their contents).<br><br>Even agents which don't have any business reading scripts (e.g. robots) have parsers which know to ignore script elements.<br><br>Mark
There are a couple more points to bear in mind though:

1) Semantically, you're supposed to be able to strip the tags out of any HTML document and have the remaining content make sense. That won't apply to a script tag if you don't enclose it in a comment.

2) Syntactically, many scripts will be invalid XHTML entities unless you enclose the script contents in a CDATA block. Otherwise a parser is entitled to treat tags within your script as part of the XML document rather than part of the script, typically breaking its well-formedness.
Well, dang. Hadn't thought about it that way.
The HTML validator for Firefox doesn't say anything about that, but it's a valid point...

[Indecision looms in the air...]
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Quote:Original post by Kylotan
1) Semantically, you're supposed to be able to strip the tags out of any HTML document and have the remaining content make sense. That won't apply to a script tag if you don't enclose it in a comment.


However, it's also understood by most HTML parser libraries that there are a few tags which need special treatment - notably script and style.

Anyone who is reading HTML programmatically should really be using a library which understands the rules of modern HTML - things like libxml2's html parser, Perl's HTML parser etc, do understand this.

Quote:
2) Syntactically, many scripts will be invalid XHTML entities unless you enclose the script contents in a CDATA block. Otherwise a parser is entitled to treat tags within your script as part of the XML document rather than part of the script, typically breaking its well-formedness.


That's absolutely true. However, in my opinion, you should not use CDATA tags, because they don't parse correctly in HTML.

Most browsers will parse XHTML documents as HTML (Ignoring any content-type and declaration)
. Therefore if you do choose to write XHTML, it MUST be compatible with these parsers.

So basically the only solution is not to use nonempty script elements at all in XHTML documents.

Mark

This topic is closed to new replies.

Advertisement