Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Is AdSense insertion via innerHTML possible?

Posted on 2010-09-05
13
Medium Priority
?
1,075 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

636 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