Solved

Is AdSense insertion via innerHTML possible?

Posted on 2010-09-05
13
1,043 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
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…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

786 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