Skip to content

Instantly share code, notes, and snippets.

@chico
Created September 21, 2012 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chico/3762293 to your computer and use it in GitHub Desktop.
Save chico/3762293 to your computer and use it in GitHub Desktop.
currency and price based on IP address
var DOLLAR = "DOLLAR";
var POUND = "POUND";
var EURO = "EURO";
function getCurrency(callback) {
$.ajax({
url: "http://api.hostip.info/get_json.php",
timeout:1000 // timeout after 1 second and default to dollars
}).done(function(json) {
var country = "";
try {
country = json["country_code"];
} catch(e) {
// whoops
}
if ("GB" == country) { // UK
doCallback(callback, POUND);
} else if (
("AD" == country) // Andorra
|| ("AT" == country) // Austria
|| ("BE" == country) // Belgium
|| ("CY" == country) // Cyprus
|| ("EE" == country) // Estonia
|| ("FI" == country) // Finland
|| ("FR" == country) // France
|| ("DE" == country) // Germany
|| ("GR" == country) // Greece
|| ("IE" == country) // Ireland
|| ("IT" == country) // Italy
|| ("LU" == country) // Luxembourg
|| ("MT" == country) // Malta
|| ("MC" == country) // Monaco
|| ("ME" == country) // Montenegro
|| ("NL" == country) // Netherlands
|| ("PT" == country) // Portugal
|| ("SM" == country) // San Marino
|| ("SK" == country) // Slovakia
|| ("SI" == country) // Slovenia
|| ("ES" == country) // Spain
|| ("VA" == country) // Vatican City
) {
doCallback(callback, EURO);
} else {
doCallback(callback, DOLLAR);
}
}).fail(function() {
doCallback(callback, DOLLAR);
});
}
function doCallback(callback, currency) {
if (typeof callback == 'function') {
callback.call(this, currency);
}
}
getCurrency(function(currency) {
var price = "";
if (DOLLAR == currency) {
price = "$25";
} else if (POUND == currency) {
price = "£15";
} else if (EURO == currency) {
price = "€19";
}
console.log("Price is " + price);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment