Skip to content

Instantly share code, notes, and snippets.

@bwedding
Created March 17, 2018 03:45
Show Gist options
  • Save bwedding/eefbdac4af57dfebf2d11a6da12047cd to your computer and use it in GitHub Desktop.
Save bwedding/eefbdac4af57dfebf2d11a6da12047cd to your computer and use it in GitHub Desktop.
Will modify any text fitting this patter {{city}}, to the current location or if it can't get location, it will change it to "in your area"
$(function()
{
var curCity = "In Your Area"; // You can change this to fit your needs
var curState = ""; // No default state
$.getJSON('//freegeoip.net/json/?callback=?',
function(location, textStatus, jqXHR)
{
if(textStatus == "success")
{
curCity = location.city;
curState = location.region_name;
}
$("body *").contents().each(function()
{
if(this.nodeType == 3) // 3 are text elements
{
// If you want to use another word
this.nodeValue = this.nodeValue.replace(/{{city}}/g, curCity);
// Uncomment this to also replace the state
// this.nodeValue = this.nodeValue.replace(/{{state}}/g, curState);
}
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment