homepage Welcome to WebmasterWorld Guest from 54.227.160.102
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Google / Google AdSense
Forum Library, Charter, Moderators: incrediBILL & jatar k & martinibuster

Google AdSense Forum

    
How insert ads based on JavaScript variable?
Taffa

5+ Year Member



 
Msg#: 13575 posted 3:18 pm on Apr 21, 2006 (gmt 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?

 

greatstart

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 13575 posted 4:03 pm on Apr 21, 2006 (gmt 0)

That violates the TOS. You are modifing the code!

celgins

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 13575 posted 4:07 pm on Apr 21, 2006 (gmt 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.

jomaxx

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



 
Msg#: 13575 posted 4:08 pm on Apr 21, 2006 (gmt 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.

inbound

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 13575 posted 4:14 pm on Apr 21, 2006 (gmt 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.

Taffa

5+ Year Member



 
Msg#: 13575 posted 4:28 pm on Apr 21, 2006 (gmt 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]-->

jomaxx

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



 
Msg#: 13575 posted 6:09 pm on Apr 21, 2006 (gmt 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.

milanmk

5+ Year Member



 
Msg#: 13575 posted 6:54 pm on Apr 21, 2006 (gmt 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

MyGen

5+ Year Member



 
Msg#: 13575 posted 7:32 pm on Apr 21, 2006 (gmt 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.

jomaxx

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



 
Msg#: 13575 posted 8:26 pm on Apr 21, 2006 (gmt 0)

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

How hard is this statement to understand?

Taffa

5+ Year Member



 
Msg#: 13575 posted 8:36 pm on Apr 21, 2006 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Google / Google AdSense
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved