Sherwin Flight
Sherwin Flight

Reputation: 2363

Validation failed because of a single character in Facebook Connect code

I am trying to use some Facebook code in one of my pages.

The code is:

<script type="text/javascript">(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=000000000";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

However when I try to validate this as an XHTML+RDFa page I get an error. The error is caused by the & sign in this part "xfbml=1&appId". The WC3 validator recommends using:

&amp;

even in urls it says.

However, when I change the ampersand sign to:

&amp;

The script no longer works.

Is there a way to get this to work correctly and still validate?

Upvotes: 0

Views: 784

Answers (2)

SeanCannon
SeanCannon

Reputation: 77976

<script type="text/javascript">
/*<![CDATA[*//*---->*/
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=000000000";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
/*--*//*]]>*/
</script>

Much of JavaScript will not be interpreted correctly by the XHTML parser. You need to escape it with CDATA.

Upvotes: 2

rjz
rjz

Reputation: 16510

You should be able to get it to validate by wrapping the code in a CDATA tag, like so:

<script type="text/javascript">(function(d, s, id) {
//<![CDATA[
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=000000000";
  fjs.parentNode.insertBefore(js, fjs);
//]]>
}(document, 'script', 'facebook-jssdk'));</script>

Upvotes: 2

Related Questions