Today I Learned - Rocky Kev

TIL using html comments in javascript

POSTED ON:

TAGS:

This is in the 'bad idea' area! But just a need little factoid!

console.log(1); <!-- log 1 -->
<!-- above statement logs 1 -->

The 'why' is because browsers without JS would ignore script tags, and is a super ancient legacy behavior.

<script>
  <!--
    // browsers without JavaScript support think this is all in a big comment,
    // and so don't render this as plain text
    alert("Hi!");
  -->
</script>

HTML comments work in JavaScript too


Related TILs

Tagged:

TIL Gmail has a 102KB size-limit for HTML

PLACEHOLDER

TIL how Error correction works in HTML

You never get an 'Invalid Syntax' error on an HTML page. Browsers fix any invalid content and go on.

TIL what DOCTYPE means

tl;dr: DOCTYPE declaration in the first line of the HTML file, to instruct the browser to run the code in Standard mode.