Welcome to WebmasterWorld Guest from 54.198.62.217

Forum Moderators: incrediBILL & martinibuster

Message Too Old, No Replies

How insert ads based on JavaScript variable?

     
3:18 pm on Apr 21, 2006 (gmt 0)

New User

10+ Year Member

joined:Oct 27, 2005
posts:17
votes: 0


I want load ads only when custom JavaScript variable is set to true.

if true...
google_ad_client = "---";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text";
google_ad_channel ="";
google_color_border = "9DADC6";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";

var x = document.createElement("script");
x.src = "http://pagead2.googlesyndication.com/pagead/show_ads.js";
document.body.appendChild(x);

Although script element is added to DOM browsers don't show nothing. Any suggestions?

4:03 pm on Apr 21, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 11, 2005
posts:668
votes: 0


That violates the TOS. You are modifing the code!
4:07 pm on Apr 21, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 12, 2006
posts:1304
votes: 0


Taffa... I'm not exactly sure what you're trying to do, but you can load different Adsense code based on whatever variable you want. For example, you can load different Adsense codes based on whether a user is using Firefox or IE; or whether or not a CSS screen background is white or black; or whatever.

However, you are not allowed to change the Adsense code itself. That is against the TOS.

4:08 pm on Apr 21, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member jomaxx is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 6, 2002
posts:4768
votes: 0


You are not allowed to change the code. You have to do this server-side, via PHP or SSI for example. If you need to have a JS component, you can put the flag into a cookie.
4:14 pm on Apr 21, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 9, 2004
posts:1435
votes: 0


A simple solution would be to use php (or something similar) to decide whether to have the script sent to the browser. That way there is no need to risk being penalised.

e.g.

if ( $your_boolean_variable == true) {
echo $AdSense_code_in_this_string;
}

You could expend the code to add whatever you like or even add an ELSE clause that uses alternative code to fill the space that AdSense would have taken up.

4:28 pm on Apr 21, 2006 (gmt 0)

New User

10+ Year Member

joined:Oct 27, 2005
posts:17
votes: 0



That violates the TOS. You are modifing the code!

I have understood that this not code modifying. I even remember that there was official answer somewhere that it doesn't matter how the JavaScript sections are added to the page as long as the actual adsense code written to the page is not modified. You can even use external .js for parameter part.

Can't use server side stuff, need to detect plug-in and if it is not present display also ads (there is lot of empty space in this case) This is mainly for IE users who are reluctant to install plug-in I'm currently using.

<!--[if IE]>
adsense code here
<![endif]-->

6:09 pm on Apr 21, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member jomaxx is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 6, 2002
posts:4768
votes: 0


IMO the requirement is to take the code generated by Google and paste it into your page unchanged. In your case the end result may appear the same once the JS is executed (if you can get it to work), but the code has clearly been changed.

Using something like PHP to write out the code conditionally is OK because the end result is indistinguishable from cut-and-pasted code to any browser or spider.

6:54 pm on Apr 21, 2006 (gmt 0)

Full Member

10+ Year Member

joined:Jan 4, 2006
posts:307
votes: 0


How about this one:

if (var == true)
{
google_ad_client = "---";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text";
google_ad_channel ="";
google_color_border = "9DADC6";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
}
<script src="googlesyndication js file"></script>

I dont know whether its against TOS or not.

Milan

7:32 pm on Apr 21, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 24, 2005
posts:121
votes: 0


Setting a simply ifelse statement is not against the TOS,


<script type="text/javascript">
if (var==true) {
ADSENSE CODE HERE;
} else { whatever you want to appear if var is false }
</script>

Obviously it would be easier to do using a serverside technology, as has already been shown.

8:26 pm on Apr 21, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member jomaxx is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 6, 2002
posts:4768
votes: 0


"code must be pasted directly into Web pages without modification"

How hard is this statement to understand?
8:36 pm on Apr 21, 2006 (gmt 0)

New User

10+ Year Member

joined:Oct 27, 2005
posts:17
votes: 0


I checked how it was done and it was different.
Whole adsense code was put inside google.js

<!--
google_ad_client = "---";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
//-->

and then was called with
<script type="text/javascript" src="google.js"></script>
(If I choose to use this method I would write this part with javascript)

Apparently this only works with IE, but thas enough.
At least [webmasterworld.com...]
says:

Its acceptable.

An email from their support.

Thank you for your email. Placing the AdSense code within a .js file is acceptable so long as there is no alteration of the AdSense code itself.

We realize that this makes the AdSense code much easier to maintain across an entire website.

App is coded with PHP so I can try Cookie approach.
Just need to figure out what to do with first time users. Maybe it is best to shows ads by default and stop only when cookie states that plug-in is installed.