4

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>
Tom Halladay
  • 5,651
  • 6
  • 46
  • 65
makopolo
  • 43
  • 3

3 Answers3

3

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>
Etienne Dupuis
  • 13,548
  • 6
  • 47
  • 58
3

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?

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

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

Tom Halladay
  • 5,651
  • 6
  • 46
  • 65