W3C validator issues with ampersand (&) in javascript location.href

When I tried to validate the code for http://www.hyips.es (one of my freelance projects) the validator kept complaining:

cannot generate system identifier for general entity langpair.

This error is caused by the validator parsing the code inside of a <script> tag. Below are the lines which were producing the error (related to a function which translates the page from spanish to english using google translator):

<script type="text/javascript"> 
function es2eng(){
  location.href = "http://translate.google.com/translate?u=" + this.location.href + "&langpair=es|en&h1=es&ie=UTF-8"; 
}
</script>
<a href="#" onclick="es2eng();"><img style="border: medium none; margin:1px;" src="/images/banderas/england-flag.gif" alt="translate to english" height="20" width="30" /></a>

After reading a lot of confussing comments (like Try to change ‘&’ to ‘&amp;’ or to ‘%26′), and just when I was about to define the function in an external js file (not a big fan of this, because the function was only one line long…), I came to this revealing post in webmasterworld.com.

The problem can be solved as easy as this: change

<script type="text/javascript">
your script is here
</script>

To:

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

The change stops W3C’s validator from parsing the code inside the script tags, therefore the error disappears! :-)

Related posts:

  1. joomla 1.5, ampersands and W3C validator
  2. CDS Invenio, OAI XML Validator: Content is not allowed in prolog
  3. Browser detection with javascript
  4. vBulletin: adsense module for vBAdvanced [SOLVED]
  5. Javascript – sort tables (by column values) [easy and opensource]

5 Responses to “W3C validator issues with ampersand (&) in javascript location.href”

  • Issac Maez says:

    Nice to see another hyip member and congrats for your payments by the way. I’ve wrote few days ago a review for this program. You may check it here

  • I’ve enjoyed reading your post about hyip. A lot of visitors are thinking this is a scam. I decided to review hyip You may check it on my site

  • Ariel says:

    I’m trying this, bu the validator still is complaining about the & and < in my JavaScript.

    Side note: w3 doesn't say to comment the cdata tag with slashes like that, although without the slashes, Dreamweaver complains about syntax errors. T_T

  • Ariel says:

    Actually, scratch that last comment. The CDATA thing does work!

  • Stephanie says:

    Thanks so much!!! I’m in the process of validating my whole site & the directions page that points to google maps was giving me a hard time. Thanks Again

Leave a Reply

Paypal donate

Please help me keep this blog up by donating.

Por favor, ayúdame a continuar con el blog donando.