makopolo
makopolo

Reputation: 43

Using comments in script link tag

What problems can be caused by placing comments in a script tag calling an external script? My coworker told me this is not a good practice.

Something like this:

<script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript">
            //    import jQuery
            //    cdn refers to a remotely hosted library
</script>

Upvotes: 4

Views: 174

Answers (3)

Tom Halladay
Tom Halladay

Reputation: 5751

Maybe your coworker is concerned you'll get in the habit of doing this

<script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript">
            //    import jQuery
            //    cdn refers to a remotely hosted library
</script>

And then eventually struggle to figure out why this doesn't work

<script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript">
     $(document).ready(function() {
           $('#ActionButton').click(DoAction);
     });
</script>

Because you've developed a bad habit

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816312

No problems at all. If the script element has a src attribute, the content is ignored.

Maybe your coworker was referring to HTML comments inside the script tag, which where used for ancient browsers which did not support JavaScript?

<script>
   <!--
      // JS was here
   // -->
</script>

Are HTML comments inside script tags a best practice?

Upvotes: 3

Etienne Dupuis
Etienne Dupuis

Reputation: 13786

Nothing wrong, maybe readability. The content will also be overwritten by the source.

<!-- import jQuery. CDN refers to a remotely hosted library -->
<script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript"></script>

Upvotes: 3

Related Questions