Solved

Is AdSense insertion via innerHTML possible?

Posted on 2010-09-05
13
1,048 Views
Last Modified: 2013-11-19
I'm not new to adsense. But I have lots of smaller Wordpress blogs that run the same desing theme and now I want to add AdSense to them too.

I want to insert AdSense code via innerHTML as the first content inside <div id="content">. The reason for this is that I dont want do be forced to edit 6-10 files (first page, page template, single post template etc). Instead I wanted to put the code above in footer.php

I thought this would work but it does not. (The excessive splitting is to avoid problems with script as string)

<script type="text/javascript"><!--
window.onload = function()
{
	var oldHmtl = document.getElementById('content').innerHtml
	var gads = '<scr' + 'ipt type="text/jav' + 'ascri' + 'pt"><' + '!' + '--\ngoogle_ad_client = "pub-XXXXXXXXXXXXXX";\n/* myCustomAdName */\ngoogle_ad_slot = "XXXXXXXX";\ngoogle_ad_width = 336;\ngoogle_ad_height = 280;\n/' + '/' + '--' + '>\n<' + '/' + 'scr' + 'ipt>\n<' + 'scr' + 'ipt type="text/' + 'jav' + 'ascri' + 'pt"\ns' + 'rc="ht' + 'tp://pagead2.google' + 'syndication.com/pagead/show_ads.j' + 's">\n' + '<' + '/' + 'sc' + 'ript>'
	document.getElementById('content').innerHtml=gads + oldHmtl
}
//-->
</script>

Open in new window


Inserting regular text works fine but not javascript.
0
Comment
Question by:Alfahane
  • 7
  • 6
13 Comments
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 33608162
That should be placed in the footer.php, or header.php file. Put the code just before the closing html tag.
0
 

Author Comment

by:Alfahane
ID: 33608332
The problem is that it does not work.
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 33608352
Are you sure you have Javascript enabled?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Alfahane
ID: 33608358
It is not rendered. Nor in Firefox or Explorer.
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 33608361
If that doesn't work, you can always use a plug-in.

http://wordpress.org/extend/plugins/all-in-one-adsense-and-ypn/  (141,922 Downloads)

http://wordpress.org/extend/plugins/adsense-manager/   (276,210 Downloads)
0
 

Author Comment

by:Alfahane
ID: 33608362
100% sure since other js works. I was thinking that maybe it's something particular with how AdSense works.
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 33608372
I couldn't say for sure, I use Analytics and if you want to insert it manually without a plug-in that is how I insert it. I wonder if your footer is being called from somewhere besides footer.php.
0
 

Author Comment

by:Alfahane
ID: 33608383
Thanks for the tip jeremyjared74. But that means I need to upload and install plugins and in that case I might just as well edit the files. I have something like 200 wordpress installations so every step saved means a lot.
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 33608398
Ok, so this is more than a tracking code? That would make sense why it isn't showing up in the footer.php. You would have to put it before the closing body tag. I just noticed you full code. Like I said I use Analytics (just code). For it to show up it has to go before the closing body tag.

If for some reason that doesn't work I did find this site that might help. The difference is it is placing it in the sidebar.php, I don't think it would matter though.

http://www.tamba2.org.uk/wordpress/adsense/
0
 

Author Comment

by:Alfahane
ID: 33609934
I think we still misunderstand each other because your link is about traditional placement of adSense code.

Let say this is the code:

[code]
<html>
<body>
.... (some stuff)
<div id="content">
Some stuff already inside "content"
</div>
.... (some stuff)

<script type="text/javascript"><!--
window.onload = function()
{
      var oldHmtl = document.getElementById('content').innerHtml
      var gads = '<scr' + 'ipt type="text/jav' + 'ascri' + 'pt"><' + '!' + '--\ngoogle_ad_client = "pub-XXXXXXXXXXXXXX";\n/* myCustomAdName */\ngoogle_ad_slot = "XXXXXXXX";\ngoogle_ad_width = 336;\ngoogle_ad_height = 280;\n/' + '/' + '--' + '>\n<' + '/' + 'scr' + 'ipt>\n<' + 'scr' + 'ipt type="text/' + 'jav' + 'ascri' + 'pt"\ns' + 'rc="ht' + 'tp://pagead2.google' + 'syndication.com/pagead/show_ads.j' + 's">\n' + '<' + '/' + 'sc' + 'ript>'
      document.getElementById('content').innerHtml=gads + oldHmtl
}
//-->
</script>

</body>
</html>
[/code]

The javascript at the end of the HTML is supposed to put adsense in the beginning (of the inside) of <div id="content">

I understand that this is not the traditional way of adding adSense.
0
 

Author Comment

by:Alfahane
ID: 33610615
I'd like to mention that the above work if
var gads  = "Some string"
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 33610879
Sorry, I don't have Adwords to test this out, so I  believe I done all I can to help you.
0
 

Accepted Solution

by:
Alfahane earned 0 total points
ID: 33639752
It seems (seems!) that AdSense is initiated window.onload. So if I call window.onload to insert it into the html the event have passed and therefore nothing fires adsense.

Once again, it seems like it.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
For many of us, the  holiday season kindles the natural urge to give back to our friends, family members and communities. While it's easy for friends to notice the impact of such deeds, understanding the contributions of businesses and enterprises i…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question