Solved

Is AdSense insertion via innerHTML possible?

Posted on 2010-09-05
13
1,030 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
Comment Utility
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
Comment Utility
The problem is that it does not work.
0
 
LVL 23

Expert Comment

by:jeremyjared74
Comment Utility
Are you sure you have Javascript enabled?
0
 

Author Comment

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

Expert Comment

by:jeremyjared74
Comment Utility
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
Comment Utility
100% sure since other js works. I was thinking that maybe it's something particular with how AdSense works.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 23

Expert Comment

by:jeremyjared74
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I'd like to mention that the above work if
var gads  = "Some string"
0
 
LVL 23

Expert Comment

by:jeremyjared74
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
Digital marketing agencies have encountered both the opportunities and difficulties that emerge from working with a wide-ranging organizations.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now