homepage Welcome to WebmasterWorld Guest from 54.227.11.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
playing around with adsense split testing code and html tags
ChanandlerBong




msg:4325365
 6:57 am on Jun 13, 2011 (gmt 0)

I'm trying to do some A/B testing using google's own javascript code:

<script type="text/javascript">
var random_number = Math.random();
if (random_number < .5){
//your first ad unit code goes here
} else {
//your second ad unit code goes here
}
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>


pretty simple to add the adsense code into that, but I want to go one step further and float one ad to the left and leave the other unfloated. I've tried everything to get my

<div style="float:left;"></div>


into this javascript and nothing works. This was the closest I got, but the result was the opening and closing tags appearing before the ad itself.


<script type="text/javascript">
var random_number = Math.random();
if (random_number < .5){
document.write('<div style="float:left;">')
<!--
google_ad_client = "ca-pub-#*$!";
/* floated left */
google_ad_slot = "#*$!";
google_ad_width = #*$!;
google_ad_height = #*$!;
//-->
document.write('</div>')
} else {
document.write('<div="margin-left:10px;">')
<!--
google_ad_client = "ca-pub-#*$!";
/* non-floated */
google_ad_slot = "#*$!";
google_ad_width = #*$!;
google_ad_height = #*$!;
//-->
document.write('</div>')
}
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>


is there something simple I'm overlooking?

added: I did sort of solve it by putting one single close /div tag outside of the whole thing, thus solving the issue of the two tags appearing together followed by the ad. There must be a cleaner way than that?

 

alias




msg:4330444
 9:58 am on Jun 24, 2011 (gmt 0)

A cleaner way to do it:
- wrap the ad code in a div with a specified id
- add one or another classname to that div using JS depending on the outcome of the random number
- use the classnames to position the ads appropriately

A sample:

<div id="ad-holder">
<script type="text/javascript">
var random = 0 | Math.random() * 10; // it's a way of getting a random number between 0 and 10
var adHolder = document.getElementById("ad-holder");
if(random > 5){
adHolder.className = "ad-a";
/* your A ad code here */
} else {
adHolder.className = "ad-b";
/* your B ad code here */
}
</script>
</div>


Hope it helps,
M.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
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