Home > Uncategorized > Determine a user’s country from their IP in #Javascript

Determine a user’s country from their IP in #Javascript

If you need a quick check to determine the country of origin of your website visitor using Javascript, then you can use this little snippet of code using the ip-api service;

  var s = document.createElement("script");
  s.src = "http://ip-api.com/json/?callback=ipFound";
  document.body.appendChild(s);
  function ipFound(ipInfo)
  {
	  alert("You are from " + ipInfo.countryCode);
  }

It’s free to use (for non-commercial purposes), and uses no API Key, It’s using JSONP which is supported on most browsers.

Evidently, if you want more foolproof check, then a server-side check would be better, but it’s good to help guide your users to the right country, or preselect some options for them.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: