0

I need to check if the current location is in Europe and if so execute some code. As there is no umbrella 'EU' code, I've had to list the countries and store them in an array. I can't figure out the best way to check against the array though...

jQuery.ajax( { 
  url: '//freegeoip.net/json/', 
  type: 'POST', 
  dataType: 'jsonp',
  success: function(location) {
    // If the visitor is browsing from UK.
    if (location.country_code === 'GB') {
      // do UK thing
    }
    // If the visitor is browsing from US.
    else if(location.country_code === 'US'){
      // do US thing
    }
    else if(location.country_code === EU){
      //do EU thing
    }
    else{
      // do nothing
    }
  }
} );


var EU = ["AT", "BE", "CY", "CZ", "DE", "DK", "EE", "ES", "FI", "FR", "GR", "HU", "IE", "NO", "NL", "PO", "SE"];
casiokid
  • 119
  • 9
  • Simple as [`indexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) – epascarello Mar 06 '15 at 14:07
  • Maybe this post is usefull:(http://stackoverflow.com/questions/6116474/how-to-find-if-an-array-contains-a-specific-string-in-javascript-jquery) – Paul Mar 06 '15 at 14:08
  • You may want [http://api.jquery.com/jquery.inarray/](http://api.jquery.com/jquery.inarray/) – lshettyl Mar 06 '15 at 14:09
  • Thanks. But I still don't understand how to write jQuery.inArray() in the above example – casiokid Mar 06 '15 at 14:20
  • Write it like: if( $.inArray( location.country_code, EU ) !== -1){ // code was found in your array, do stuff here... } then put your other if else statements after that. inArray returns -1 if the element is not found in the array so if it returns anything other than -1 you know it was found in the array. Just make sure you declare the array before you call this code – Wesley Smith Mar 06 '15 at 14:37

0 Answers0