Forum Moderators: martinibuster

Message Too Old, No Replies

Insert AdSense by Javascript

New layout is heavy javascript based

         

jetteroheller

9:38 pm on Dec 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I just changed to a complete new layout.

In the basic layout are fixed 2 AdLinks and 2 other AsSense ads.

The new navigation is creted by javascript.

I would like to insert in the big navigation field
an AdSense ad.

But innerHtml and document.write did not work to do it

Anybody successfull inserts AdSense code by javascript on a page?

onebuyone

9:08 am on Dec 18, 2011 (gmt 0)

10+ Year Member



I'm not sure if I understand question correctly. Do you want to load ads after page was loaded? If yes, then here is some old "hack", I used it few years ago and I'm not sure if it will work today, I also didn't ever contact AdSense support if it's "legal" to use it, so use it on your own risk.


window["google_ad_client"] = 'pub-...';
/* rest of ad params */

window.adcontainer = document.getElementById('containerid');
window.adhtml = '';

function mywrite(html) {
adhtml += html;
if(html == '</iframe>')
adcontainer.innerHTML = adhtml;
};

document.write_ = document.write;
document.write = mywrite;

script = document.createElement('script');
script.src='http://pagead2.googlesyndication.com/pagead/show_ads.js';
script.type='text/javascript';
document.body.appendChild(script);


This way you can load ads at any moment after page got loaded, however if it wont dramaticaly increase your income, better don't use it, because it is against TOS imo, so it might be not worth it.

jetteroheller

6:34 pm on Dec 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks! AFAIK TOS says, "The code has to be not modified, as is from copy the code.

Normal, I copy the coed in my CMS.

Where I want to have the code in my page, I write for example
<text=google.300x250> and the CMS copies the code from my 300x250 ad here.

In case of the javaxript, the code is copied into a javascript variable and is also direct inserted.

The idea why to use javascript is to automatic decide depending on the length of the content.

When the content is long enough, that scrolling down at normal screen size, the other ads above the fold are no longer visible,
I would like to insert a 728x90

The second idea is sequence of load.
I want this last ad to be loaded later, first should be content and the ads above the fold.