• flickr
  • linkedin
  • twitter
  • rss
Get country from IP address with JavaScript

Get country from IP address with JavaScript

This uses the telize site to get a geolocation of an IP address. It’s not 100% accurate but fine for country-level location, which is all I wanted.

<script type="application/javascript">
 function getgeoip(json){
 //document.write("Geolocation information for IP address : ", json.ip);
 //document.write("Country : ", json.country);
 //document.write("Latitude : ", json.latitude);
 //document.write("Longitude : ", json.longitude);
 //document.write("Country: " + json.country);
 //tellCP(json.country); // this function sent the country off to captivate 8.

 }</script>

<script type="application/javascript" src="http://www.telize.com/geoip?callback=getgeoip"></script>

If of interest, here’s the tellCP function. I then used the page after the splash to run an advanced action on enter, using the supplied country to customise some stuff in my module based off the learners’ country.

function tellCP(country)///tell captivate 8 that we have the var, and send it on through.
	{
			//alert(country);
			if(window.cpAPIInterface) {
				alert('working');
				window.cpAPIInterface.setVariableValue("userCountry", country);
				window.cpAPIInterface.next();

			} else{
				alert('not working');	
			}
	}

NB you’ll need to work around the default resume bookmarking thing in Captivate for this to work all the time – still playing with it!

Tech Reference: ,

Larry's a 30-something chap interested in travel, being a dork and changing the world via less boring training.

Leave a reply