Sign in to follow this  

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

This topic is 3869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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 javascript 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 javascript to create totals and such)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 javascript, still understand script tags (i.e. that they should ignore their contents).

Even agents which don't have any business reading scripts (e.g. robots) have parsers which know to ignore script elements.

Mark

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 3869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this