954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

JavaScript + Google Adsense Problem

One of my affiliates is having a problem with his Google Adsense and JavaScript.

It works about 2/3's of the time. But on other occasions it gives an undefined error or only one ad shown.

The original code is as follows:

function google_ad_request_done(google_ads) {  
	if (google_ads.length > 0) {
		var first_ad_unit = '', second_ad_unit = '';
		for(var i = 0; i < google_ads.length; ++i) {
			if (i < 4) {
				if (i == 0) first_ad_unit += 'First Ad Unit:<table width="100%"><tr>';
				first_ad_unit += '<td valign="top" nowrap><b>AD #: ' + (i+1) + '</b><a href="' + google_ads[i].url + '"><b>' + google_ads[i].line1 + '</b></a>' + google_ads[i].line2 + '' + google_ads[i].line3 + '<font color="#008000">' + google_ads[i].visible_url + '</font></td>';
				if (i == 3) first_ad_unit += '</tr></table>';
			} else {
				if (i == 4) second_ad_unit += 'Second Ad Unit:';
				second_ad_unit += '<p><b>AD #: ' + (i+1) + '</b><a href="' + google_ads[i].url + '"><b>' + google_ads[i].line1 + '</b></a>' + google_ads[i].line2 + '' + google_ads[i].line3 + '<font color="#008000">' + google_ads[i].visible_url + '</font></p>';
			}			
		
		}
	}
	
	document.getElementById("first_ad_unit").innerHTML += first_ad_unit;
	document.getElementById("second_ad_unit").innerHTML += second_ad_unit;
	
}


google_ad_client = 'YOUR_CLIENT_ID';
google_ad_output = 'js';
google_max_num_ads = '8';
google_adtest = 'on';
google_safe = 'high';
google_feedback = "on";
google_ad_type  = "text_image";

Newly Configured Code:

function google_ad_request_done(google_ads) {  
	if (google_ads.length > 0) {
		var first_ad_unit = '', second_ad_unit = '', third_ad_unit = '';
		for(var i = 0; i < google_ads.length; ++i) {
			if (i < 3) { 
				if (i == 0) first_ad_unit += '<table width="710"><tr>';
				first_ad_unit += '<td width="33%" valign="top" nowrap><a class="adsense" href="' + google_ads[i].url + '" onMouseover="window.status=\'http://' + google_ads[i].visible_url + '\'; return true"><b>' + google_ads[i].line1 + '</b></a><a class="adsensedesc" href="' + google_ads[i].url + '" onMouseover="window.status=\'http://' + google_ads[i].visible_url + '\'; return true">' + google_ads[i].line2 + '</a><a class="adsensedesc" href="' + google_ads[i].url + '" onMouseover="window.status=\'http://' + google_ads[i].visible_url + '\'; return true">' + google_ads[i].line3 + '</a></td>';
				if (i == 2) first_ad_unit += '</tr></table><table width="710"><div align="right"><a class="adsensedesc" href="' + google_info.feedback_url + '">Ads by Google</a></div></table>';
			} else {
				second_ad_unit += '<a class="adsense" href="' + google_ads[i].url + '" onMouseover="window.status=\'http://' + google_ads[i].visible_url + '\'; return true"><b>' + google_ads[i].line1 + '</b></a><a class="adsensedesc" href="' + google_ads[i].url + '" onMouseover="window.status=\'http://' + google_ads[i].visible_url + '\'; return true">' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '</a>';
				if (i == 3) third_ad_unit += '<div align="right"><a class="adsensedesc" href="' + google_info.feedback_url + '">Ads by Google</a></div>';
			}			
		
		}
	}
	
	document.getElementById("first_ad_unit").innerHTML += first_ad_unit;
	document.getElementById("second_ad_unit").innerHTML += second_ad_unit;
	document.getElementById("third_ad_unit").innerHTML += third_ad_unit;
	
}


google_ad_client = 'removed for privacy';
google_ad_output = 'js';
google_max_num_ads = '24';
google_safe = 'high';
google_feedback = "on";
google_ad_type  = "text_image";
google_ad_channel ="removed for privacy";


Any Help at all is welcome. Like I said before it works about 2/3's of the time. But there must be some reason it comes up with errors the other times.

Thanks,
Lightninghawk

Lightninghawk
Posting Whiz in Training
291 posts since Jun 2005
Reputation Points: 35
Solved Threads: 9
 

Hi,

Where is the part where google_ad_request_done(google_ads); is called?

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

Hi,

Where is the part where google_ad_request_done(google_ads); is called?


function google_ad_request_done(google_ads) {

Then it starts Logic If Statements.

Lightninghawk
Posting Whiz in Training
291 posts since Jun 2005
Reputation Points: 35
Solved Threads: 9
 

What I mean is, where is the function, "google_ad_request_done", being called.

The reason I ask is that it will show the parameter "google_ads".

From the function code, you can tell that google_ads will most likely be an Array, containing Objects with properties: url, visible_url, line1 etc.

But unless we see that part of the code, it cannot be debugged...

Is this code from google adsense by the way? or did someone write it to show multiple google adsense ads?

Looks very interesting...

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

PM me your email address and I will send you the Zip file with all that information.

Lightninghawk
Posting Whiz in Training
291 posts since Jun 2005
Reputation Points: 35
Solved Threads: 9
 

Hi please let me know where the function "google_ad_request_done" is called.
My email is [removed]

Thanks

vipin.mathur
Newbie Poster
1 post since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

thas is my address email i want you to send me the Zip and all information. of that plz [removed]

mplok
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You