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">
user2838753
  • 41
  • 2
  • 9

3 Answers3

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • I use other javascripts on my website, those don't give errors and I am not using CDATA there. Thnx for the tip! – user2838753 Nov 14 '13 at 15:38
  • @user2838753 you may not have that strict XHTML doctype in the other pages. Really, if you don't have a particular reason for XHTML, just use the HTML5 doctype. – Pointy Nov 14 '13 at 15:38
  • 1
    @user2838753: Alternatively, you might just not be using `<`, `>`, or `&` operators or characters in scripts on that page. – rninty Nov 14 '13 at 15:39
1

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

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

Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109
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.

rninty
  • 1,072
  • 6
  • 10
  • Yes, I first used HTML5 and when I changed it to xHTML this error appeared. I read some discussions about XHTML vs HTML5. And for some website xhtml is better (they say). – user2838753 Nov 14 '13 at 15:42
  • @user2838753: That is nearly never true. If you want to restrict yourself to HTML4 elements and attributes for compatibility, you can do that in HTML5. You can also add the trailing `/>` to void tags as a style choice. Either way, though, using an external script solves the problem neatly and also reduces load, because external scripts can be cached. – rninty Nov 14 '13 at 15:45
  • On this moment I use PHP to include the javascript. But if I change this to a .js external script the script doesnt work anymore?! – user2838753 Nov 14 '13 at 15:56