Enable and disable Google AdSense code for different environments.
Information
You can earn money by putting Google ads on your website. You just need to put a few lines of
javascript code in your html page. The content of the ads are automatically generated
by the code and is based on the content of the page (= contextual ads).
This guide describes how to put AdSense code in a single javascript file.
By doing it this way you do not need to change all your web pages, only one javascript file.
Furthermore you can disable the actual Google Ads and instead display a dummy image ad in your development or test environment thus avoiding accidental clicks.
Procedure
- Put the following code in a javascript file, e.g.: google.js.
var display_ads = true;
function google(size) {
var scripttext = "";
if (display_ads) {
scripttext = '<script type="text/javascript"><!--\n'
+ 'google_ad_client = "enter_your_id";\n'
+ 'google_alternate_ad_url = "http://www.mydomain.com/alternate.html";\n'
}
// Text Image Ad
if (size == "728x90") {
if (display_ads) {
scripttext += 'google_ad_width = 728;\n'
+ 'google_ad_height = 90;\n'
+ 'google_ad_format = "728x90_as";\n'
+ 'google_ad_type = "text_image";\n'
+ 'google_ad_channel = "enter_your_channelid";\n'
+ 'google_color_border = "6F84B9";\n'
+ 'google_color_bg = "6F84B9";\n'
+ 'google_color_link = "D7E0ED";\n'
+ 'google_color_text = "FFFFFF";\n'
+ 'google_color_url = "D7E0ED";\n';
} else {
scripttext = '<img src="/banner728x90.gif" border="0" />';
}
}
// Link Unit Ad
if (size == "468x15") {
if (display_ads) {
scripttext += 'google_ad_width = 468;\n'
+ 'google_ad_height = 15;\n'
+ 'google_ad_format = "468x15_0ads_al";\n'
+ 'google_ad_channel ="enter_your_channelid";\n'
+ 'google_color_border = "FFFFFF";\n'
+ 'google_color_bg = "FFFFFF";\n'
+ 'google_color_link = "0000FF";\n'
+ 'google_color_text = "0000FF";\n'
+ 'google_color_url = "0000FF";\n';
} else {
scripttext = '<img src="/banner468x15.gif" border="0" />';
}
}
// Text Image Ad
if (size == "300x250") {
if (display_ads) {
scripttext += 'google_ad_width = 300;\n'
+ 'google_ad_height = 250;\n'
+ 'google_ad_format = "300x250_as";\n'
+ 'google_ad_type = "text_image";\n'
+ 'google_ad_channel ="enter_your_channelid";\n'
+ 'google_color_border = "000000";\n'
+ 'google_color_bg = "FFFFFF";\n'
+ 'google_color_link = "0000FF";\n'
+ 'google_color_text = "FFFFFF";\n'
+ 'google_color_url = "0000FF";\n';
} else {
scripttext = '<img src="/banner300x250.gif" border="0" />';
}
}
if (display_ads) {
scripttext += '//--></script>\n'
+ '<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">\n'
+ '</script>\n';
}
return scripttext;
}
function display_ad_top_right() {
var scriptval = "";
scriptval = google('728x90');
return scriptval;
}
- In your html code, add the following lines:
<html>
<head>
<title>demo</title>
<script type="text/javascript" language="JavaScript" src="/google.js"></script>
</head>
<body>
:
<script type="text/javascript" language="JavaScript">
//<![CDATA[
document.write(display_ad_top_right());
//]]>
</script>
<noscript>
<img src="/banner.gif" border="0" />';
</noscript>
:
</body>
</html>
- Explanation:
-
display_ads = true;
The google ads are displayed.
-
display_ads = false;
The google ads are not displayed instead the
alternative banner images "bannerWxH.gif"are displayed. This option is added
to switch off google ads in a development or test environment,
so testers can not accidently click on these links.
-
scriptval = google('728x90');
By changing the value '728x90' into '468x15' or '300x250' you can determine which ad to display.
-
<noscript> .. </noscript>
It is recommended to set the <noscript> tags incase the browser javascript functionality is
disabled.
|