SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Use an external function as a variable?

    Hi -

    I know very little about Javascript. I'm trying to use a function to specify a condition for an if statement. I can't edit the function because it is external.

    Specifically; I am trying to use a geolocation service (geoPlugin to geolocate your visitors) to taylor part of my wesite. The service provides javascript functions here.

    I am only interested in knowing which continent the visitor is from -

    function geoplugin_continentCode() { return 'EU';}

    In my case this function returns EU. What I want is to trigger a simple alert box onload but only where the continent code shows NA (which - I think - it will do if a visitor to my website is based in North America.)

    I can't figure out if there is a way to use a function return as a variable if one can't edit the function? Or is this just something I can't do with Javascript?

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Its actually pretty simple, all you need to do is set a var to reference the function and alert the newly set var value.

    Code JavaScript:
    var cc = geoplugin_countryCode();
    alert(cc);

  3. #3
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks... I'm probably being really thick here, but where in this do I specify the function's return (ie the continent code to differentiate between regions)?

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You can do something like the below...

    Code JavaScript:
    var cc = geoplugin_countryCode();
     
    switch (cc) {
        case 'US':
            // United States
        break;
        case 'AU':
            // Australia
        break;
        case 'CN':
            // China
        break;
        default:
            // All other countries
    }


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •