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
  • 1
    What is your doctype? Commented Nov 14, 2013 at 15:34
  • 1
    Is this in a <script> block? Commented Nov 14, 2013 at 15:34
  • Doctype added in start post. Yes, I use a script block. Also added in start post. Commented Nov 14, 2013 at 15:34
  • 1
    Use an external script. Commented Nov 14, 2013 at 15:35

3 Answers 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.

Sign up to request clarification or add additional context in comments.

3 Comments

I use other javascripts on my website, those don't give errors and I am not using CDATA there. Thnx for the tip!
@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.
@user2838753: Alternatively, you might just not be using <, >, or & operators or characters in scripts on that page.
1

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

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

Comments

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.

3 Comments

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: 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.
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?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.