Created
January 9, 2013 21:15
-
-
Save anonymous/4497017 to your computer and use it in GitHub Desktop.
Basic jQuery Geo Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
$.geo = function(callback){ | |
var that = this; | |
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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