Skip to content

Instantly share code, notes, and snippets.

@yahelc
Forked from anonymous/jQuery-geo.js
Last active December 10, 2015 21:48
Show Gist options
  • Save yahelc/4497028 to your computer and use it in GitHub Desktop.
Save yahelc/4497028 to your computer and use it in GitHub Desktop.
(function($){
$.geo = function(callback){
if(!$.geo.data){
$.ajax({
cache: true,
url: '//www.google.com/jsapi',
dataType: "script",
success: function(){
try{
$.geo.data = google.loader.ClientLocation.address;
}
catch(e){
$.geo.data = {city: null, region: null, country: null, country_code:null};
}
callback.call(this, $.geo.data);
}
});
}
else{
callback.call(this, $.geo.data);
}
};
}(window.jQuery));
/* Pass a function to $.geo; the argument will be an object featuring the geo data, or null attributes if for some reason there's no geo data*/
$.geo(function(data){
if(data.region === "NY"){
$("#lightbox").text("Hello New York!");
}
else if(data.region === "CA"){
$("#lightbox").text("Wes' coast!");
}
else if(!data.region){
//no geo data
}
});
/* Repeated usage will use cached values, so it only loads the JS once. */
$.geo(function(data){
if(data.country_code && data.country_code !=="US"){
$("#welcome").text("Buenos noches!");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment