0

I get the following error with validator.W3.org

Line 70, Column 26: character "<" is the first character of a delimiter but occurred as data

if (remainingSeconds < 10) {

Line 70, Column 26: StartTag: invalid element name

if (remainingSeconds < 10) {

This is the code I use.

<script type="text/javascript">
function secondPassed() {
var minutes = Math.round((seconds - 30)/60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
    remainingSeconds = "0" + remainingSeconds;  
}
</script>

If I delete the < or change the < to a = then the error is gone.

Anybody an idea?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4

3 回答 3

2

You're validating an XHTML document, so you have to use CDATA markers around your script contents.

<script>
  <![CDATA[
  // JavaScript goes here
  ]]>
</script>

Personally I don't know why you'd use XHTML in this day and age, but whatever.

于 2013-11-14T15:36:23.167 回答
1

If you are validating as XHTML, try wrapping your JavaScript in <![CDATA[]]> blocks:

What does <![CDATA[]]> in XML mean?

于 2013-11-14T15:36:36.377 回答
1

You’re using strict XHTML, so all characters that are part of the XML syntax (pretty much) have to be escaped properly if you want to use them as text. In XHTML, the usual way to do that is with a CDATA block that’s escaped in a way to be compatible with HTML:

<script type="text/javascript">//<![CDATA[
function secondPassed() {
var minutes = Math.round((seconds - 30)/60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
    remainingSeconds = "0" + remainingSeconds;  
}
//]]></script>

However, there are two better fixes:

  • Use HTML5 (<!DOCTYPE html>), not XHTML
  • Use an external script, <script type="text/javascript" src="second-passed.js"></script>

Both are better practice.

于 2013-11-14T15:37:42.520 回答