views:

429

answers:

2

Hello, I have this bit of jQuery at the top of my page (used for a simple image carousel):

$(document).ready(function(){ 
 $("#slider").easySlider({
  prevText:'<div id="backarrow">Back</div>',
  nextText:'<div id="nextarrow">View Other Projects</div>',
  orientation:'horizontal'
 });
});

however, I can't get it to validate XHTML strict:

Line 12, Column 33: document type does not allow element "div" here

Any ideas?

+9  A: 
<script type="text/javascript">
/* <![CDATA[ */
$(document).ready(function(){   
    $("#slider").easySlider({
            prevText:'<div id="backarrow">Back</div>',
            nextText:'<div id="nextarrow">View Other Projects</div>',
            orientation:'horizontal'
    });
});
/* ]]> */
</script>

This tells the validator to interpret the script as character data, not markup, and thus it won't parse the structure of the CDATA block. Wikipedia has more info.

ceejayoz
Awesome! Thanks
Tylor
http://dorward.me.uk/www/comments-cdata/ explains the history of this.
David Dorward
A: 

It has nothing to with JQuery. Just surround the JS with comments (CDATA as ceejayoz gave should work too).

Matthew Flaschen
Comments, if the XHTML is processed as XHTML, will comment out the script so it will not run. Do NOT use them for this.
David Dorward
David, I just tested in Firefox 3 with a XHTML 1.0 Strict (fully validating) page containing : <script type="text/javascript"> <!-- alert("foo"); --> </script> You're simply wrong.
Matthew Flaschen
Did you serve the document as application/xhtml+xml? I'm betting not. If you serve it as text/html then it will be processed as HTML not XHTML. The rules for HTML are not the same as those for XHTML.
David Dorward
I stand corrected. When served as XML (note, this is not required to be valid XHTML), the script will not run.
Matthew Flaschen