What does it mean in html / javascript?

I went through a problem where a tag was presented <script> and it contained these HTML comment characters.

Function example

<html>
  <head>
<script type="text/javascript">
<!--
  function Alert(){
  alert("apenas uma função exemplo")
  }
//-->
</script>
    </head>
  <body>
    <button onClick="Alert()">Funcão Exemplo</button>
    </body>
      </html>

If you put this structure in an application ASP.NET WebForms, and simply remove the tag comment from the end of the script, it does not work.

<html>
  <head>
<script type="text/javascript">
<!--
  function Alert(){
  alert("apenas uma função exemplo")
  }
 
</script>
    </head>
  <body>
    <button onClick="Alert()">Funcão Exemplo</button>
    </body>
      </html>
Author: Bacco, 2016-06-21

3 answers

This is aHTML comment tag . It starts with !-- and ends with --. Without this comment the parser of HTML can get lost.

It is common to use it to" hide " a script JavaScript. Only this will get complicated with this -- HTML. So use // which is the JavaScript comment to ignore this code which is basically HTML.

Noticed that it looks like one thing but at the bottom are two different constructions, and more, of languages different?

If you do not have this you will consider that the comment is not finished and everything else will be disregarded as useful HTML code.

This is an excellent reason not to use this kind of thing. In fact nowadays this is much less used. The general recommendation is not to have a JavaScript code inside the HTML. Create an external file and include it by traditional means. It is more organized, has several advantages besides avoiding this confusion.

This is independent the technology you are using on the server.

Examples:

Tem um texto aqui <!-- the middle of --> com comentário no meio.

<!--
Pode ter
várias linhas de
comentário
-->

<div class="exemplo">
Pode ter uma construção normal e simular o *inline* desde que abra e feche.<br>
</div> <!-- /.exemplo -->

I put on GitHub for future reference.

 8
Author: Maniero, 2020-01-23 13:48:14

Used for Javascript, it is just an end-of-function orientation consensus. As for example comments to mark beginning and end of a certain code delimitation, I use this myself to identify myself.

Look if this helps.

function teste(){
   alert('sou um robô');  
}
// **>
<div><!-- div do conteudo x -->
 <p>yyyyyyyyyy</p> 
</div> <!-- fim do conteudo da div x -->
 2
Author: Pedro Franco, 2016-06-21 13:32:05

This is an HTML code comment. As far as I know this has been used in the past, to make browsers that did not support Javascript have no problems with this "new tag" <script>.

 2
Author: Wallace Maxters, 2016-06-21 19:55:26