1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
$().ready(function() { $.ajax({ url: "https://jsonip.com", data: { format: 'json' }, error: function(jqXHR, textStatus, errorThrown) { if (textStatus == "timeout") { console.log(textStatus + ': ' + errorThrown); } }, dataType: 'json', crossDomain: true, success: function(json) { $.ajax({ url: "http://ip-api.com/json/" + json.ip, data: { format: 'json' }, dataType: 'json', crossDomain: true, success: function(geoData) { jsn = JSON.stringify(geoData, undefined, 2); document.getElementById("position").innerHTML = jsn; } }); }, }); }); |
Well, Javascript is beautiful jQuery and json is the food for stomach. I cannot come up with anything better but here is how you can get your website visitor geolocation based on his IP.
I was first thinking to recursively call the ajax function to pull IP and then geo data based on IP. But I later realize since both Json is of different structure. Doing that would be too great of a thing. So there you go nice and neat (y)
0 Comments
Your comment will be posted after it is approved.
Leave a Reply. |
![]()
Custom Search
Other Blogs & PagesGit Commands Animation & VFX SitesA MUST READ for Ani/VFX Artistsawakened by thoughts,
|