I using some GeoIP service to place country flag on pages depends on country IP. And I need to cache JSON response for all pages on my site.
This code placed into
You could use localStorage like that:
var smartIp = JSON.parse(localStorage.getItem('smartIp'));
if (!smartIp) $.getJSON('http://smart-ip.net/geoip-json?callback=?', function (data) {
smartIp = localStorage.setItem('smartIp', JSON.stringify(data));
});
DEMO
So, in your specific case, you should use this code in your header.php page:
var smartIp = JSON.parse(localStorage.getItem('smartIp'));
if (!smartIp) $.getJSON('http://smart-ip.net/geoip-json?callback=?', function (data) {
smartIp = localStorage.setItem('smartIp', JSON.stringify(data));
$('#flag').html("
");
});
else $('#flag').html("
");