Forum Moderators: martinibuster

Message Too Old, No Replies

Javascript for Dynamic AdSense size - allowed?

Making ad size depend on available space

         

ZoneMR

11:18 am on Jan 28, 2006 (gmt 0)

10+ Year Member



I have a series of sites on which I have added adsense to the right-hand-side sidebar.

The height of the content varies significantly. On some pages, there are only two paragraphs, whereas on others there is significantly more.

I want to use 125x125 AS blocks on the short pages, 120x240 on the med-length pages, and 120x600 on the long ones. This is to avoid large skyscrapers hanging down way past the text, or small ad-blocks with empty space below them.

This works nicely:


<script type="text/javascript"><!--
<!-- Decide on ad size based on content height -->
content_height = document.getElementById("column-content").offsetHeight;
sidebar_height = content_height - 25;
//Default dimentions
google_ad_width = 120;
google_ad_height = 240;
google_ad_format = "120x240_as";
//For long pages
if(sidebar_height > 600){
google_ad_height = 600;
google_ad_format = "120x600_as";
}
//For short pages
if(sidebar_height < 240) {
google_ad_width = 125;
google_ad_height = 125;
google_ad_format = "125x125_as";
}
//Show the adsense block
<!--- Start of standard AdSense code -->
google_ad_client = "pub-blahblahblah";
google_alternate_color = "ffffff";
google_ad_type = "text";
google_ad_channel ="123456789";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "000099";
google_color_url = "000099";
google_color_text = "000000";
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Technically it's modifying the AdSense code - however the intention obviously isn't malicious. Has anyone got a reply from Google if something similar is allowed?

Of course an alernative would be to re-structure the code so that the original AdSense code is left untouched, but simply wrapped in "if" or "switch" constructs. However this would seem less elegant code-wise, (duplication rather than code reuse).

[edited by: ZoneMR at 11:31 am (utc) on Jan. 28, 2006]

Woz

11:27 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>Technically it's modifying the AdSense code

I would think that is your answer, but best to check with the Adsense people to be sure.

Onya
Woz

humblebeginnings

11:37 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I understand the intention is OK, but I don't think the Google algo cares about you being a nice guy;-)
Better call them. It would surprise me if they say it's allowed cause there might thousands of other folks with good intentions planning to modify the AS code in their own way. BTW, I think your modification is a very interesting one. G should opt it in.
Keep us posted.

ZoneMR

3:31 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



Thanks for your advice. I've submitted the guestion to the AdSense team, and will let you know of a reply.

RonPK

4:53 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're using getElementById(), which is not supported by some ancient browsers like Netscape 4 and IE 4. In those browsers the code will be broken, so I guess AS might object, allthough hardly anybody uses these browsers nowadays.

I'm doing something similar (show / hide the DIV with the AS code depending on screen width), and I've never had any complaints from G (knocks on wood).

jomaxx

5:05 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The answer will almost certainly be no. Maybe you can do something similar in PHP, perhaps using file size to roughly distinguish between shorter and longer pages.

ZoneMR

7:48 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



The answer will almost certainly be no. Maybe you can do something similar in PHP, perhaps using file size to roughly distinguish between shorter and longer pages.

Using PHP to do it is what I'm considering as a last resort. The number of characters in the text is a poor estimate of its rendered height, which will depend on the point at which browsers wrap lines, the resolution and window size, chosen font size, and other client-side factors. The Javascript solution is elegant, and results in neatly laid-out pages with ads that addapt nicely and don't stick out like a sore thumb.

Thanks for your comment on getElementById - although hardly anyone still uses V4 browsers, it won't cost me a lot of time to fix the script to support them.

I realise that the script in it's current form might not be accepted by google, as they probably won't make an exception to their "don't edit our code" policy.

However, hopefully they will accept it in a slightly modified form, which leaves their original code untouched.

if(smallPage) {
original, untouched google code for small adblock
} elseif(medPage)
original, untouched google code for large adblock
}
etc

[edited by: ZoneMR at 7:54 pm (utc) on Jan. 28, 2006]

vincevincevince

7:51 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's a major health search/portal which does this, but they are a premium publisher.

jomaxx

11:36 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW I do think making certain layout decisions based on available screen space is a great idea in principle, and I do it on my homepage.

Not with ads, though; I vary the amount of content I present so as to prevent the need for side-scrolling.