Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JS: If/Else statement with an Array

Options
  • 20-07-2006 11:39am
    #1
    Registered Users Posts: 3,514 ✭✭✭


    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

    [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"&gt;
    </script>[/html]

    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?
    [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

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


Comments

  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    I'm not sure what you're doing - it seems a little strange to me, and your text doesn't really match what you're saying, but if all you want to do is set the variable google_cpa_choice from the array, depending on the browser, then the following is the best way to do it. I much prefer object detection as a mechanism to determine the browser type, rather than searching for the text in then navigator object.

    You also have a bizarre non-working src= statement - what's that all about? Anyway, have a look at this and see if it does what you need.
    <script type="text/javascript">
    	var iearray = new Array(2);
       iearray[0] = "CAAQveHnzwEaCEiH5iXPqf1SKJe193M";
       iearray[1] = "CAAQ6OGIlAIaCFN_N8Za4RF6KITHrYMB";
    
       var index = 0;  // Default = the first entry
       if (document.getElementById && !document.all) { // NS6 or Firefox
          index = 1;
       } else if (document.getElementById) {           // IE5+
          index = 0;
       } else if (document.all) {                      // IE4+
          index = 0;
       } else if (document.layers) {                   // NS4
          index = 1;
       } else if (window.opera) {                      // Opera
          index = 1;
       }
       google_cpa_choice = iearray[index];
    </script>
    

    Edit: fixed a typo in the code.


Advertisement