Home > Uncategorized > Retrieving external IP address and geolocation of an IP address using JavaScript

Retrieving external IP address and geolocation of an IP address using JavaScript


Yesterday I was working on a client application which required to read the public address of the client and their geo-location. So after some search I wrote the following html page which gets the current IP as well as retrieves the public location of an IP purely using JavaScript and some external APIs.

01 <html>
02 <head>
03 <title>Locate an IP</title>
04 <script type="text/javascript" src="yqlgeo.js"></script>
05 <script type="text/javascript">
06
07 var ipaddress;
08
09 //get location of an IP
10 function getIPLocation() {
11 document.getElementById('divDetails').innerHTML = '<br />Loading..';
12 try {
13 var start = new Date();
14 var ctrl = document.getElementById('txtIP');
15 var ipadd = ctrl.value;
16 yqlgeo.get(ipadd, function (o) {
17 var details = '<br /><h3><b>Location Details</b></h3>';
18 details += 'Country Code : <b>' + o.place.country.code + '</b><br />';
19 details += 'Country Name : <b>' + o.place.country.content + '</b><br />';
20 details += 'Country Language : <b>' + o.place.lang + '</b><br />';
21 details += 'Latitude : <b>' + o.place.centroid.latitude + '</b><br />';
22 details += 'Longitude : <b>' + o.place.centroid.longitude + '</b><br />';
23 details += 'Place Name : <b>' + o.place.name + '</b><br />';
24 document.getElementById('divDetails').innerHTML = details;
25 });
26 var end = new Date();
27 var diff = dateDifference(start, end);
28 //document.getElementById('divtime').innerHTML = 'Time Elapsed : ' + diff.seconds;
29 }
30 catch (e) {
31 document.getElementById('divDetails').innerHTML = e.Message;
32 }
33 }
34
35 //get current IP
36 function getCurrentIP() {
37 var ctrl = document.getElementById('txtCurrentIP');
38 if (ctrl) {
39 ctrl.value = ipaddress;
40 }
41 else
42 alert('Unable to retrieve IP');
43 }
44
45 function getip(json) {
46 if (json.ip)
47 ipaddress = json.ip;
48 else
49 alert('Unable to retrieve the IP address');
50 }
51
52 </script>
53 <script type="text/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>
54 </head>
55 <body>
56 <form id="frm" action="javascript:getIPLocation()">
57 <p>
58 Get your IP Address
59 </p>
60 <input type="text" id="txtCurrentIP" />
61 <input type="button" onclick="getCurrentIP()" value="Get IP" />
62 <p>
63 Type the IP Address
64 </p>
65 <input type="text" id="txtIP" />
66 <input type="button" onclick="getIPLocation()" value="Locate" />
67 <br />
68 <div id="divDetails">
69 </div>
70 <br />
71 <div id="divtime">
72 </div>
73 </form>
74 </body>
75 </html>

The sample makes use of the YQL Geo Library written by Chris Heilmann which is available here to retrieve the geo-location of an IP. To retrieve the client’s external IP I’ve made use of the API provided by jsoip.appsot.com. Thanks to the SO post here.

 

Advertisements
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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: