[html] Javascript warning and error of w3c

There are some javascript in below of the page:

<script type="javascript">
var test = 4;
if(test < 5)
{
alert(
"Test w3c");
}
</script>

When test the page in http://validator.w3.org/#validate_by_uri, we got the warning and error:

Line X, ColumnX: XML paring Error: StartTag: invalid element name
if(test
< 5)

Why?

We can get the reason here: http://www.w3.org/TR/2002/REC-xhtml1-20020801/#h-4.8

The resolved:

<script type="javascript">
/*<![CDATA[*/
var test = 4;
if(test < 5)
{
alert(
"Test w3c");
}
/*]]
>*/
</script>

Add the "<![CDATA[" and "]]>" to the code!

原文地址:https://www.cnblogs.com/davidhhuan/p/1740137.html