Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1082
  • Last Modified:

Is AdSense insertion via innerHTML possible?

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
Alfahane
Asked:
Alfahane
  • 7
  • 6
1 Solution
 
jeremyjared74Commented:
That should be placed in the footer.php, or header.php file. Put the code just before the closing html tag.
0
 
AlfahaneAuthor Commented:
The problem is that it does not work.
0
 
jeremyjared74Commented:
Are you sure you have Javascript enabled?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
AlfahaneAuthor Commented:
It is not rendered. Nor in Firefox or Explorer.
0
 
jeremyjared74Commented:
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
 
AlfahaneAuthor Commented:
100% sure since other js works. I was thinking that maybe it's something particular with how AdSense works.
0
 
jeremyjared74Commented:
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
 
AlfahaneAuthor Commented:
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
 
jeremyjared74Commented:
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
 
AlfahaneAuthor Commented:
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
 
AlfahaneAuthor Commented:
I'd like to mention that the above work if
var gads  = "Some string"
0
 
jeremyjared74Commented:
Sorry, I don't have Adwords to test this out, so I  believe I done all I can to help you.
0
 
AlfahaneAuthor Commented:
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

Sign your company up to try the MB 660 headset now

Take control and stay focused in noisy open office environments with the MB 660. By reducing background noise, you can revitalize your office and improve concentration.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now