Forum Moderators: open

Message Too Old, No Replies

Failing W3C Validation on Javascript

         

terrybarnes

8:07 pm on Jul 15, 2009 (gmt 0)

10+ Year Member



I have this IE PNG fix on my Wordpress Blog header but it's failing validation with the following code:

if(($.browser.msie)&(parseInt($.browser.version)<7)){

It's failing with the & and the < with the following:

Warning Line 62, Column 24: character "&amp;" is the first character of a delimiter but occurred as data

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

Error Line 62, Column 24: XML Parsing Error: xmlParseEntityRef: no name

Error Line 62, Column 53: XML Parsing Error: StartTag: invalid element name

Any idea on how I can resolve this issue so it passes - it's a bit frustrating as it's the only thing I'm having trouble with.

pageoneresults

8:08 pm on Jul 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Escape the ampersand?

&amp;

terrybarnes

8:17 pm on Jul 15, 2009 (gmt 0)

10+ Year Member



Sorry, I don't follow. Escape the ampersand? Do you mean write it as:

if(($.browser.msie)&amp;(parseInt($.browser.version)<7)){

using &amp; instead of &

Thanks, by the way, for getting replying so quickly.

terrybarnes

8:34 pm on Jul 15, 2009 (gmt 0)

10+ Year Member



I went ahead and changed to:

if(($.browser.msie)&amp;(parseInt($.browser.version)&lt;7)){

but now in IE6 its saying there's an error on this line: The error is: Expected ')'

$("img[src$='.png']").each(function(){$(this).addClass("png");});

pageoneresults

8:38 pm on Jul 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I may be the wrong person to be giving advice on this one. Usually when the validator fails and there is an ampersand involved, it's due to the ampersand not being escaped e.g. & vs &amp; and I'm not sure if that is the case here. I'll let those with real JS experience come to your rescue. :)

terrybarnes

10:57 pm on Jul 15, 2009 (gmt 0)

10+ Year Member



The thing is if I write "&" as a "&amp" although it then passes validation it creates an error in IE6, as explained in my previous post... Very strange!

whoisgregg

1:32 am on Jul 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are using XHTML and have ampersands in your scripts, your javascript block needs to be commented out in order to validate (or you need to use an external script). Like this:

<script type="text/javascript">
<![CDATA[
... unescaped script content ...
]]>
</script>

Source: [w3.org...]

Fotiman

3:16 am on Jul 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The best and easiest solution is simply to put the JavaScript in a separate .js file. Keeps the JavaScript (behavior) code out of your markup.

terrybarnes

8:54 am on Jul 16, 2009 (gmt 0)

10+ Year Member



Thanks for everyone's help with this - I ended up going with Fotiman's suggestion as this was the only way that the script would a) work and b) pass validation. I attempted the CDATA suggestion and although this helped pass validation it failed to perform the function in IE6