What does <!-- //--> mean in html / javascript?

Asked

Viewed 563 times

4

I went through a problem where was presented a tag <script> and 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 ASP.NET Webforms application, and simply remove the tag comment from the end of script, it doesn’t 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>

  • <!-- comentário --> = HTML --- /* comentario */ = Javascripts, css, Asp and php

  • where did you see this? play on w3c to see what it is @Marcos Costa

  • @Assanges in a legacy system of the company where I work.

3 answers

8


That’s a tag html comment. It starts with !-- and ends with the --. Without that comment the parser HTML can get lost.

It is common to use it to "hide" a script Javascript. Only this will be complicated with this -- HTML. Then use // which is the Javascript comment for ignoring this code which at the bottom is HTML.

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

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. Actually 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. Gets more organized, has several advantages besides avoiding this confusion.

This is independent of 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 in the Github for future reference.

2

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

See 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

That’s an HTML code comment. As far as I know this was used in the past, to make browsers that did not have Javascript support not have problems with this "new tag" <script>.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.