Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily comment out <script> 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.

like image 440
zkent Avatar asked Apr 03 '14 14:04

zkent


1 Answers

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

like image 185
CodingIntrigue Avatar answered Sep 29 '22 04:09

CodingIntrigue