Skip to main content

Trouble Adding AdSense Code for Page-Level Ads? Learn how to fix it

In order to add the Page-Level ads functionality from AdSense on your Blogger blog you are required to place a code that AdSense provides which is :

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"/>

Unfortunately if you have tried placing the code in your theme you may have run into this error that states:

That's a bit annoying - What could be going wrong with the code that AdSense (a Google product) has provided is causing error on Blogger (another Google product).

The problem is highlighted in the error message itself "Attribute name "async" associated with an element type "script" must be followed by the ' = ' character."

Attribute name "async" in the code we got must be followed by a '=' character. But the code we got simply has <script async  ... />

So what can be done? Modify the code to be parsed by Blogger's theme parser correctly. Here's the code with a little change, we replaced async with async="async" to make the code a valid XHTML markup.

<script async="async" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

Place this code in your Blogger theme code and save. It should work properly now.
Why do we have to use this "hack"?
This shouldn't even be considered as a hack. This is plainly a case where a valud HTML5 markup is not parsed as a valid XML / XHTML markup.

Yes, our blogs are HTML5 in the end when it gets loaded on a web browser, but the code that runs in the background is XHTML, and the thing with XHTML is that it is very strict about the markup language. In HTML5 you can simply leave out a value from a attribute or leave an HTML element unclosed and it will still work.

But XHTML won't. What's valid in XHTML is also valid in HTML5, so this don't need to be considered a hack or even a workaround. AdSense provides the code that is totally HTML5 compliant only it didn't anticipate the case where users may want to add the code to XHTML templates.
Will it have any impact on the way this code is supposed to work?
Other than having a few extra characters in the code you had to add, no. This change wouldn't affect anything how AdSense is supposed to work. We just made the code compliant with XHTML to be taken in by Blogger.

Comments

Popular posts from this blog

How to draw a diagonal line on table in Microsoft Word 2007-2013-2016

Most of us who have to use the Microsoft word 2007-2010-2013-2016, of course, that means you drawed a table. But to have a good and nice table is not simple.

Blogger’s new templates: Contempo, Soho, Emporio and Notable

For a while now, I’ve been increasingly conscious that the design of Law Actually has become a bit long in the tooth. I’ve occasionally tinkered with the look and feel of my blog over the last few years, but there’s been no escaping the fact it was based on a (now pretty primitive) blogger design from six or seven years ago. The last major refresh I’d made to the design was in 2012. Yikes. I made a few minor changes earlier this year when I suddenly remembered I used to do (and quite enjoy) something called blogging. Don’t look at me like that: apparently, blogging simply isn’t such a big thing anymore.  Despite, the need for a visual change, I rather sadly recognised that it was well beyond my web design skills to produce something half-decent myself. Although blogger templates are available from third parties, they tend to be infested with problems – including advert placements and other awkward elements which are difficult to remove or modify. After a few very disappointin...