Google Share Button Does Not Validate – Not Valid HTML

So you have run your site through the Nu Html Checker  and everything is tickity-boo except the Google share button.  Bah! You get an error that looks like this:

The text content of element script was not in the required format: Expected space, tab, newline, or slash but found { instead.

If, like me, you wanted to change the language of the share button and ended up with some code that looks like this:

<script src=”https://apis.google.com/js/platform.js” async defer>{lang: ‘en-US’}</script>

…then this will be where the problem lies.  Here is why and how to fix it.

Why

Simple really, script tags can either have a src attribute and be empty, or have code/content within the tag and have no src attribute.  In the case above it has both a src attribute and content within the <script></script> tags.   This, therefore breaks the valid script tag rules and will not validate – no matter if you start trying to shove in CDATA, add additional quotes, or anything else.

How To Fix

To fix the problem we need to remove the content from within the <script></script> tags and place them somewhere else.  However, just doing the following DOES NOT WORK:

<script src=”https://apis.google.com/js/platform.js” async defer></script>
<script>{lang: ‘en-US’}</script>

Now that the value is outside of the platform.js script tag, Google does NOT pick up the value and the language does not change. Doh.

What you need to do is shove the data into the Google object on the page.  Don’t worry it is easy:

<script src=”https://apis.google.com/js/platform.js” async defer>
<script> window.___gcfg = { lang: ‘en_US’, };  </script>

This will now validate (yes!) and the Google button will now display in the correct language (double yes!).  Job done.

 

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Apply your human brain cells and complete this highly complicated maths problem *