fixing Google AdSense

R

Roedy Green

Google Adsense is a scheme where by Google pays you to put ads on your
website. see http://mindprod.com/jgloss/google.html#ADSENSE

The catch is, the javascript they use makes the pages unusable when
the adserver is not available either because google is down, the link
to Google is down, or you are browsing pages offline.

I know nothing of JavaScript. I have written to google about the
problem and got one of those thanks for sharing form letters.

I wondered if it what I want them to do is technically possible --
suppress the ads when offline or the server does not respond in say 3
seconds.

Presuming Google does not fix the problem, I wondered if I could wrap
or modify the JavaScript myself to make this work, or perhaps use some
other technique.


Here is the JavaScript they use:

(function(){

function m(b){return b!=null?'"'+b+'"':'""'}

function C(b){if(typeof
encodeURIComponent=="function"){return
encodeURIComponent(b)}else{return escape(b)}}

function B(b,a,d){var f=a.indexOf("?");
var e="http://pagead2.googlesyndication.com/pagead/imp.gif?event=";
e+=d;
if(f!=-1&&f+1<a.length){e+="&"+a.substring(f+1)}var h='<img height="1"
width="1" border="0" src='+m(e)+"/>";
b.write(h)}

function c(b,a){if(a){window.google_ad_url+="&"+b+"="+a}}

function g(b,a){if(a){c(b,C(a))}}

function l(b,a,d){if(a&&typeof
a=="object"){a=a[d%a.length]}c("color_"+b,a)}

function E(b,a){var d=b.screen;
var f=navigator.javaEnabled();
var e=-a.getTimezoneOffset();
if(d){c("u_h",d.height);
c("u_w",d.width);
c("u_ah",d.availHeight);
c("u_aw",d.availWidth);
c("u_cd",d.colorDepth)}c("u_tz",e);
c("u_his",history.length);
c("u_java",f);
if(navigator.plugins){c("u_nplug",navigator.plugins.length)}if(navigator.mimeTypes){c("u_nmime",navigator.mimeTypes.length)}}

function
y(b){b=b.toLowerCase();
if(b.substring(0,3)!="ca-"){b="ca-"+b}return
b}

function
H(b,a,d){d=d.substring(0,1000);
d=d.replace(/%\w?$/,"");
if(b.google_ad_output=="js"&&(b.google_ad_request_done||b.google_radlink_request_done)){a.write('<script
language="JavaScript1.1" src='+m(d)+"></"+"script>")}else
if(b.google_ad_output=="html"){if(b.name=="google_ads_frame"){B(a,d,"reboundredirect")}else{a.write('<iframe
name="google_ads_frame" width='+m(b.google_ad_width)+"
height="+m(b.google_ad_height)+"
frameborder="+m(b.google_ad_frameborder)+" src="+m(d)+'
marginwidth="0"'+' marginheight="0"'+' vspace="0"'+'
hspace="0"'+' allowtransparency="true"'+'
scrolling="no">');
B(a,d,"noiframe");
a.write("</iframe>")}}}

function G(b){var a=null;
b.google_ad_frameborder=a;
b.google_ad_format=a;
b.google_page_url=a;
b.google_language=a;
b.google_gl=a;
b.google_country=a;
b.google_region=a;
b.google_city=a;
b.google_hints=a;
b.google_safe=a;
b.google_encoding=a;
b.google_ad_output=a;
b.google_max_num_ads=a;
b.google_ad_channel=a;
b.google_contents=a;
b.google_alternate_ad_url=a;
b.google_alternate_color=a;
b.google_color_bg=a;
b.google_color_text=a;
b.google_color_link=a;
b.google_color_url=a;
b.google_color_border=a;
b.google_color_line=a;
b.google_adtest=a;
b.google_kw_type=a;
b.google_kw=a;
b.google_num_radlinks=a;
b.google_max_radlink_len=a;
b.google_rl_filtering=a;
b.google_rl_mode=a;
b.google_rt=a;
b.google_ad_type=a;
b.google_image_size=a;
b.google_feedback=a;
b.google_skip=a;
b.google_page_location=a;
b.google_referrer_url=a;
b.google_ad_region=a;
b.google_ad_section=a;
b.google_bid=a;
b.google_cpa_choice=a}

function A(){var b=null;
var a=window;
var d=document;
var f=new Date();
var e=f.getTime();
var h=a.google_ad_format;
if(a.google_cpa_choice){a.google_ad_url="http://pagead2.googlesyndication.com/cpa/ads?";
a.google_ad_url+="client="+escape(y(a.google_ad_client));
a.google_ad_region="_google_cpa_region_";
c("cpa_choice",a.google_cpa_choice)}else{a.google_ad_url="http://pagead2.googlesyndication.com/pagead/ads?";
a.google_ad_url+="client="+escape(y(a.google_ad_client))}var
k=a.google_num_slots_by_client;
var w=a.google_num_slots_by_channel;
var j=a.google_prev_ad_formats_by_region;
a.onerror=a.google_org_error_handler;
if(a.google_ad_region==b&&a.google_ad_section!=b){a.google_ad_region=a.google_ad_section}var
i=a.google_ad_region==b?"":a.google_ad_region;
var q=false;
if(h){q=h.indexOf("_0ads")>0}if(q){if(a.google_num_0ad_slots){a.google_num_0ad_slots=a.google_num_0ad_slots+1}else{a.google_num_0ad_slots=1}if(a.google_num_0ad_slots>1){return}}else
if(!a.google_cpa_choice){if(a.google_num_ad_slots){a.google_num_ad_slots=a.google_num_ad_slots+1}else{a.google_num_ad_slots=1}if(a.google_num_slots_to_rotate){j=b;
if(a.google_num_slot_to_show==b){a.google_num_slot_to_show=e%a.google_num_slots_to_rotate+1}if(a.google_num_slot_to_show!=a.google_num_ad_slots){return}}else
if(a.google_num_ad_slots>3&&i==""){return}}c("dt",f.getTime());
c("hl",a.google_language);
if(a.google_country){c("gl",a.google_country)}else{c("gl",a.google_gl)}c("gr",a.google_region);
g("gcs",a.google_city);
g("hints",a.google_hints);
c("adsafe",a.google_safe);
c("oe",a.google_encoding);
c("lmt",a.google_last_modified_time);
g("alternate_ad_url",a.google_alternate_ad_url);
c("alt_color",a.google_alternate_color);
c("skip",a.google_skip);
var n=a.google_ad_client;
if(!k[n]){k[n]=1;
k.length+=1}else{k[n]+=1}if(j){g("prev_fmts",j.toLowerCase());
if(k.length>1){c("slot",k[n])}}if(h){g("format",h.toLowerCase());
if(j){j=j+","+h}else{j=h}}c("num_ads",a.google_max_num_ads);
c("output",a.google_ad_output);
c("adtest",a.google_adtest);
if(a.google_ad_channel){var r=a.google_ad_channel.toLowerCase();
g("channel",r);
var s="";
var t=r.split("+");
for(var o=0;
o<t.length;
o++){var p=t[o];
if(!w[p]){w[p]=1}else{s+=p+"+"}}g("pv_ch",s)}g("url",a.google_page_url);
l("bg",a.google_color_bg,e);
l("text",a.google_color_text,e);
l("link",a.google_color_link,e);
l("url",a.google_color_url,e);
l("border",a.google_color_border,e);
l("line",a.google_color_line,e);
c("kw_type",a.google_kw_type);
g("kw",a.google_kw);
g("contents",a.google_contents);
c("num_radlinks",a.google_num_radlinks);
c("max_radlink_len",a.google_max_radlink_len);
c("rl_filtering",a.google_rl_filtering);
c("rl_mode",a.google_rl_mode);
c("rt",a.google_rt);
c("ad_type",a.google_ad_type);
c("image_size",a.google_image_size);
c("region",a.google_ad_region);
c("feedback_link",a.google_feedback);
g("ref",a.google_referrer_url);
g("loc",a.google_page_location);
c("bid",a.google_bid);
if(z(a,d)&&d.body){var u=d.body.scrollHeight;
var v=d.body.clientHeight;
if(v&&u){g("cc",Math.round(v*100/u))}}E(a,f);
H(a,d,a.google_ad_url);
G(a)}

function D(b,a,d){A();
return true}

function z(b,a){return b.top.location==a.location}

function x(b,a){var d=a.documentElement;
if(z(b,a))return
false;
if(b.google_ad_width&&b.google_ad_height){var f=1;
var e=1;
if(b.innerHeight){f=b.innerWidth;
e=b.innerHeight}else
if(d&&d.clientHeight){f=d.clientWidth;
e=d.clientHeight}else
if(a.body){f=a.body.clientWidth;
e=a.body.clientHeight}if(e>2*b.google_ad_height||f>2*b.google_ad_width){return
false}}return true}

function F(){var b=window;
var a=document;
var d=a.location;
var f=a.referrer;
var e=null;
b.google_org_error_handler=b.onerror;
b.onerror=D;
if(b.google_ad_frameborder==e){b.google_ad_frameborder=0}if(b.google_ad_output==e){b.google_ad_output="html"}if(b.google_ad_format==e&&b.google_ad_output=="html"){b.google_ad_format=b.google_ad_width+"x"+b.google_ad_height}if(b.google_page_url==e){b.google_page_url=f;
if(!x(b,a)){b.google_page_url=d;
b.google_last_modified_time=Date.parse(a.lastModified)/1000;
b.google_referrer_url=f}}else{b.google_page_location=f;
if(!x(b,a)){b.google_page_location=d}}if(b.google_num_slots_by_channel==e){b.google_num_slots_by_channel=new
Array()}if(b.google_num_slots_by_client==e){b.google_num_slots_by_client=new
Array()}if(b.google_prev_ad_formats_by_region==e){b.google_prev_ad_formats_by_region=new
Array()}}

F();
A();
})()
 
M

Martin Honnen

Roedy said:
Google Adsense is a scheme where by Google pays you to put ads on your
website. see http://mindprod.com/jgloss/google.html#ADSENSE

The catch is, the javascript they use makes the pages unusable when
the adserver is not available either because google is down, the link
to Google is down, or you are browsing pages offline.

Browsers like IE or Firefox have a setting to browse offline, I don't
think they will make any requests to servers in that setting, be that
for script files, ad images or other resources. So browsing offline with
the proper setting on the browser shouldn't be the problem.

And what does unusable mean? I could understand that page display is
much delayed when such a script is not available, the browser has to
wait with further rendering until it has the script and has it executed
so there can be delays then until the connection times out and the
browser continues without having the script.
 
R

Roedy Green

And what does unusable mean? I could understand that page display is
much delayed when such a script is not available, the browser has to
wait with further rendering until it has the script and has it executed
so there can be delays then until the connection times out and the
browser continues without having the script.

In the Opera browser which I use most often, it won't completely
render the page below the ad until it has the ad contents. It will sit
there for a very long time before it gives up maybe a minute or so.
It does not "finish" rendering the page and give control back to you.

I have 24-7 internet access, so most of the time it works fine.
However I encourage people to mirror my site with the Replicator
software and browse offline for speed.

I run into a similar problem with webrings. See
http://mindprod.com/jgloss/webring.html
 
V

VK

Roedy said:
I wondered if it what I want them to do is technically possible --
suppress the ads when offline or the server does not respond in say 3
seconds.

Technically possible if it's for offline browsing - thus it's run on
local drive - thus you may bypass cross-domain security limitations for
ads iframe (say getting error messages from it).

By did you try more simple way by setting defer flag?

for conventional HTML:
<script type="text/javascript" src="myScript.js" defer></script>
or for XHTML (if you bother to use it):
<script type="text/javascript" src="myScript.js"
defer="defer"></script>

This way the page will be rendered w/o delay, and script will fail
seemlessly on the background.
 
R

Roedy Green

By did you try more simple way by setting defer flag?

I did not write that code. Google did. I don't even know how to read
it, other than in a generic way since I speak Java, and many other
computer languages, but not JavaScript.
 
V

VK

Roedy said:
I did not write that code. Google did. I don't even know how to read
it, other than in a generic way since I speak Java, and many other
computer languages, but not JavaScript.

I did not mean to change the code itself but set the "defer" flag for
interpreter. In Java terms it would be like javac myProg.java -defer
(there is not such flag in Java but the mechanics is close).
"defer" is supported by the majority of browsers made over the last few
years (IE, FF and Opera 8.x inclusive). It tells to interpreter that
the code to execute doesn't change the page immediately during initial
loading and that it doesn't provide any vital parts needed to render
the page during the initial loading. This way the page is being
rendered at the normal speed w/o waiting script results.

JavaScript code is being inserted into page in two ways: either inline
or as separate .js file:

<script type="text/javascript" defer>
// your code
// ...
</script>

<script type="text/javascript" src="myScript.js" defer></script>

Again: no changes needed in the code itself. If any problems please
give us a link to some HTML page you want to destribute.
 
M

Martin Honnen

Roedy said:
I did not write that code. Google did.

He was simply suggesting to use
<script type="text/javascript" defer>
// script code goes here
</script>
respectively
<script type="text/javascript" src="file.js" defer></script>
in your HTML documents where you include the script. Not a change to the
script itself.
That defer attribute however is something only IE supports and anything
the script will output (with document.write) then will end up at a
different location than before as IE will treat the script differently
<http://msdn.microsoft.com/library/d...p/author/dhtml/reference/properties/defer.asp>
and defer its execution till the HTML of the page has been parsed.
 
M

Matt Kruse

Roedy said:
The catch is, the javascript they use makes the pages unusable when
the adserver is not available either because google is down, the link
to Google is down, or you are browsing pages offline.

I've never seen evidence that a page becomes 'unusable'.

What I do hate about AdSense, though, is that the body's onLoad doesn't fire
until it's loaded, and it's often slow.

A while ago, I messed around with a consistent way to defer the loading of
the iframe by re-assigning the document.write function to actually store the
google content in a variable, which I then wrote out later after the page
was loaded. It was quite a hack, though.

Is there a more solid solution for consistently deferring the adsense
content load so the body's onLoad event fires without waiting for the google
content to load?
 
J

Jim Ley

Is there a more solid solution for consistently deferring the adsense
content load so the body's onLoad event fires without waiting for the google
content to load?

Your better bet is to just avoid doing stuff onload - there's very
little that's relevant to onload other than doing "is all content
available so hide the loading... div", which use case requires the
google ads are loaded too.

Jim.
 
M

Matt Kruse

Jim said:
Your better bet is to just avoid doing stuff onload - there's very
little that's relevant to onload other than doing "is all content
available so hide the loading... div", which use case requires the
google ads are loaded too.

A lot of "unobtrusive javascript" techniques wait until the page is loaded
to then go back and alter the page contents with extended js functionality.

The alternative is to just load the script at the bottom of the page after
your content, but
1) Loading all scripts in the <head> is cleaner
and
2) There is no spec that says the DOM objects must be available to script as
soon as they are parsed, so waiting until the document is loaded and
available is the best approach

Or do you have a differing opinion?
 
J

Jim Ley

A lot of "unobtrusive javascript" techniques wait until the page is loaded
to then go back and alter the page contents with extended js functionality.

Yes, they're flawed, because the user gets inconsistent,
non-deterministic behaviour until after all the content comes in. As
you have noticed with adsense on slow connections it's really poor.

The "head is cleaner" is just silly, if we were actually going for
cleanliness we wouldn't be using the mess that is HTML/JS developing,
if it's really a problem for you, just generate a pre-processor. But
I refuse to accept:

<html>
<head><script ... ></head>
<body>
... content ... content ...
<script ...>
page_load()
</script>
</body>
</html>

is going to significantly impact any cleanliness measure. Of course
even that isn't perfect you still get delays, but at least it's
considerably less delays as there's no images etc. being downloaded.

The lack of a standard argument is also pretty silly, you can easily
deal with that by object detection and also doing it onload.

Jim.
 
M

Matt Kruse

Jim said:
The "head is cleaner" is just silly

I disagree.
If for no other reason than distribution of generalized scripts to a large
audience. Including a script before </body> is, in my experience, something
that confuses a lot of people. Also, in some web applications the user may
have access to adding scripts to the <head> but that's it, because the rest
of the page is driven by a template engine or something.

But I also realize those are pretty weak arguments ;)
The lack of a standard argument is also pretty silly, you can easily
deal with that by object detection and also doing it onload.

I'm not sure that's possible. If your script scans the document for items
with a certain class, for example, the user agent may allow you to see the
'document' object and run getElementsByTagName but return no results. At
that point, it's impossible to know whether it needs to be run again on page
load or if there just aren't any elements in the page.

Nevertheless, in many applications and web sites onLoad is used frequently.
I would still be interested in knowing about a better method of deferring
the adsense load so it doesn't delay the firing of the onload event.
 
J

Jim Ley

I'm not sure that's possible. If your script scans the document for items
with a certain class, for example, the user agent may allow you to see the
'document' object and run getElementsByTagName but return no results. At
that point, it's impossible to know whether it needs to be run again on page
load or if there just aren't any elements in the page.

Of course you can, you just run it again regardless, and only perform
the action on those you didn't the first time.
Nevertheless, in many applications and web sites onLoad is used frequently.

Yes, but it's better to fix that, which will improve your users
experience rather than try to solve a symptom:
I would still be interested in knowing about a better method of deferring
the adsense load so it doesn't delay the firing of the onload event.

All content delays firing the onload event, so I hope you're also
doing things like empty images which you swap in place on load too.

Jim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Google appscript 0
How to play corresponding sound? 2
Blue J Ciphertext Program 2
My Status, Ciphertext 2
Google sheets 0
Google sheets 2
How to treat an input data as variable? 4
Python3 string slicing 2

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top