/    Sign up×
Community /Pin to ProfileBookmark

JS: If/Else statement with an Array

Hi guys. i have signed up google adsense and i’m using their referral adverts for firefox and picasso but i only want to display one referral at a time on a page. I don’t see much point in advertising the firefox referral to users who are already viewing the page using firefox so is it possible to write some JS which will only display the firefox referal to users of non firefox browsers and that will also display both the firefox and picasso referrals to all other users (IE/Safare/Netscape/Opera)

I’m currently using an If/Else statement

[code=html]
<SCRIPT language=”JavaScript”>
<!–
google_ad_client = “pub-7213854248425086”;
google_ad_width = 125;
google_ad_height = 125;
google_ad_format = “125x125_as_rimg”;
google_ad_channel = “”;
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”;

if (navigator.userAgent.indexOf(“Firefox”)!=-1) {
google_cpa_choice = “CAAQ6OGIlAIaCFN_N8Za4RF6KITHrYMB”;
} else {
google_cpa_choice = “CAAQveHnzwEaCEiH5iXPqf1SKJe193M”;
}
–>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>[/code]

The code above works fairly well.
Firefox users -> Picasso Ad (no Firefox Ad)
IE/Opera users -> Firefox Ad

However i want to be able to disable the Picasso Advert to IE users as well as the Firefox Advert. I was thinking the best way to do it would be to create an array of 2 and store the google_cpa_choice value within the array. Then use a random funtion on the array to disable the values if the user is using IE.

My JS isn’t the best though, and the attempts i have made have being horrible. Could someone provide me with a few pointers?

[code=html]
var iearray = new Array(2)
iearry[0] = “google_cpa_choice = “CAAQveHnzwEaCEiH5iXPqf1SKJe193M”
iearry[1] = “google_cpa_choice = “CAAQ6OGIlAIaCFN_N8Za4RF6KITHrYMB”

//code
//code
else {
google_cpa_choice = document.write(iearry) //random

}
[/code]

^ this is in no way correct but is a bit pseudocode-esque on what i want to accomplish.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Megatron spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...