Pergunta

I am getting this error in my markup validation:

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

for (var i = 0; i < array.length; i++) {

Is there something wrong with the "<" character in the following js?

function preloadImages(array) {
if (!preloadImages.list) {
    preloadImages.list = [];
}
for (var i = 0; i < array.length; i++) {
    var img = new Image();
    img.src = array[i];
    preloadImages.list.push(img);
}
}
Foi útil?

Solução

It looks like your document is XHTML and your Javascript is inlined. At least you seem to be validating against XHTML. Since XHTML is rather strict and closer to XML, < is interpreted as the start of a new tag and confused the validator.

There is a number of ways to fix this:

  • Use HTML5 instead of XHTML; really, this usually is the best choice unless you have a good reason to stick to XHTML. It is understood by all major browsers and is the latest and most modern doctype that allows all the cool new features of the Web 2.0. The doctype for HTML5 looks like this:

    <!DOCTYPE html>
    <html>
    ....
    
  • Don't inline your Javascript, but add it as an external resource. You should be doing this all the time anyway, no matter what doctype you are using:

    <script type="text/javascript" src="yourScript.js"></script>
    
  • Enclose the script in a CDATA block:

    <script type="text/javascript">
    <![CDATA[
        // your code
    ]]>
    </script>
    

You can read up on doctypes and XHTML for further details.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top