Temporarily Comment out Tag for Debugging [Closed]

Temporarily Comment out Tag for Debugging [Closed]

Often while coding and debugging I might want to comment out a <script> tag. An example might be when doing the following:

<script src="lib/jquery.js"></script>
<!--script src="lib/jquery.min.js"></script-->

I tend to add the new line instead of just changing the original to act as a reminder that I want to put that back before going live. I got this syntax from a colleague but I had never seen this it before. Is there a syntactically correct method to comment out <script> tags in HTML?

EDIT: I know there are lots of discussions about commenting out scripts in order to hide them from older browsers but that is not what I am doing. I am wanting to hide the tag completely.

6

1 Answer

One option would be to dynamically load your scripts in, given a debug flag. For example:

Markup:

<script src="lib/include.js"></script>

include.js

var IS_DEBUG = true;

if(IS_DEBUG) {
    loadScript("jquery.js");
    loadScript("anotherscript.js");
}
else {
    loadScript("jquery.min.js");
    loadScript("anotherscript.min.js");
}

function loadScript(name) {
    var elem = document.createElement("script");
    elem.src = name;
    document.getElementsByTagName("head")[0].appendChild(elem);
}

That means you can just toggle the IS_DEBUG flag to load in the required scripts. This is a very rudimentary example, but you get the idea. You might even be able to tie this in with something like require.js

2
Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.