Migrating away from our legacy GeoIP JavaScript services

Our legacy GeoIP JavaScript services are end of life as of October 31, 2017. This document is to help you make the transition away from them.

What should you do?

There are several options available. The recommended option is to switch to performing lookups on your server via an integration with our GeoIP2 Precision web services. Another option is to switch to using our GeoIP2 downloadable databases. Both of these options allow you to perform GeoIP2 lookups on your server rather than in the browser.

You could also switch to using our GeoIP2 JavaScript client. While this is the most direct equivalent to our legacy GeoIP JavaScript services, we recommend that you use one of the server side integrations instead. With server side integrations, lookups do not need to be performed in the browser, so they are inherently more secure. Using a server side integration is the most future proof option.

Switching to our GeoIP2 JavaScript client

This section will show you how to convert a very simple website using our legacy GeoIP JavaScript services to our new GeoIP2 JavaScript client.

We recommend that you have an experienced developer familiar with JavaScript make such updates.

Before you make any updates, there are two key differences you should be aware of:

  1. The data provided by our GeoIP2 services has some differences from our legacy GeoIP services. You can read more about the differences here.
  2. The biggest difference you will notice about our GeoIP2 JavaScript client is that you receive lookup results via callback functions rather than through direct function calls. This is a common pattern in JavaScript, but it means you may need to re-structure your code.

An example legacy GeoIP JavaScript integration

Here is a very simple website that uses our legacy GeoIP JavaScript services to show the city and country names of the visitor:

If you load this page, it would show something like this:

Converting it to a GeoIP2 JavaScript integration

In order to convert this website to use our GeoIP2 JavaScript client, there are two main things to do:

  1. You must switch the first <script> element’s src URL to our GeoIP2 JavaScript file.
  2. You must use callback functions rather than direct function calls.

An easy way to switch to callback functions in the above example is to replace the locations with geoip_*() function calls to have elements that you can access and update from a JavaScript function. We can do this by changing the last two <script> elements to be <span> elements with id attributes.

Once you have these elements, you can write your callback functions. The success callback will have the results of the lookup. You can take these results and update the elements with the information.

Here is how that would look:

If you load this page, you will see the same text before.

References and further reading

  • We have a live demo using our GeoIP2 JavaScript client here.
  • We have a tutorial about how to use our GeoIP2 JavaScript client here.
  • We have documentation about our GeoIP2 JavaScript services here.
  • What’s New in GeoIP2