Solved

Comment tags in JavaScript

Posted on 2014-11-11
5
189 Views
Last Modified: 2014-11-12
Hi,

i have a task to clean up javascript / html comments from jsp code, preferably via regex.
I found something like this:

<script language="JavaScript">
                <!--
                openPopupDialog("xxx");
                // -->
</script>

And i'm unsure what this actually is. Is this something for old IE versions?

Thanks for you help
0
Comment
Question by:Fabian Maass
5 Comments
 
LVL 10

Assisted Solution

by:JEaston
JEaston earned 100 total points
ID: 40435261
As far as I know this is to support older browsers - i.e. one's which do not support the <script> tags.  Unless you are supporting very old clients it shouldn't be needed.
0
 
LVL 5

Accepted Solution

by:
meb_santoso earned 400 total points
ID: 40435273
HTML comments, ie. <!-- -->, are no longer needed. They were intended to allow (very old) browsers that didn't understand the <script> tag to degrade gracefully (see it as a fallback scenario).

These (very) old browsers are just like dinos, we practically can't find it anywhere, so frankly it's pointless to use the <!-- -->.
Do note that IF you explicitly still want to use comments before and after the javascript function, place double slashes before the closing comment tag:

<!--
blablablablabal
//-->

This prevents JavaScript from executing the --> tag.

...or use commented out CDATA tag:

<script type="text/javascript">
//<![CDATA[
document.write("Hello World!");
//]]>
</script>

Can you delete those <!-- --> lines? Go ahead, unless there are actual comments in there that is needed for debugging purposes.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40435996
If you're wanting help with the regex, you're probably best to ask that in a separate question. There is a "Regular Expressions" topic area that is well attended.
0
 

Author Closing Comment

by:Fabian Maass
ID: 40436792
thank you all for your feedback.

so, to summarise:

1. using <!-- ... --> inside a javascript function was intended for old browsers, yet it is NOT a comment but the commands inside are evaluated
2. valid comments inside a javascript function should use the CDATA tag
3. the correct way to use html comments inside a javascript function would be <!-- //--> as the greater than sign will be evaluated otherwise

i think there is a misunderstanding on my part regarding the double slash. The answers say "HTML comments, ie. <!-- -->, are no longer needed.", yet i was not asking for tags " --> " but for " // --> ".

Please confirm my summary ;)

Thank you all
0
 
LVL 5

Expert Comment

by:meb_santoso
ID: 40436831
Summary 1,2,3 are accurate.

Regarding the "// -->" : it's a mixed up world, between "I want to write (HTML) comments with the <script> </script> so I can humbrag about my functions that I copy-pasted from some other script" and "No, javascript, please ignore the dash-dash-greaterThan, just leave it alone".

It was a good practice back in the days where you want to do the above to think of a fallback scenario for the older browsers, but as JEaston also wrote, it's no longer needed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now