A few code snippets to Get Client IP Using JavaScript/jQuery.

1. One way

This one gives you longitude/latitude and timezone.

Try it!

$(document).ready( function() { $.getJSON( "http://smart-ip.net/geoip-json?callback=?", function(data){ alert( data.host); } );});


   source: "smart-ip.net",
   host: "",
   lang: "en",
   countryName: "Australia",
   countryCode: "AU",
   city: "South Sydney Municipality",
   region: "New South Wales",
   latitude: "-33.9000",
   longitude: "151.2000",
   timezone: "Australia/NSW"

2. Two way

This one provides JSON.

Try it!

function myIP() {
    if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
    else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("GET"," http://api.hostip.info/get_html.php ",false);
    hostipInfo = xmlhttp.responseText.split("n");
    for (i=0; hostipInfo.length >= i; i++) {
        ipAddress = hostipInfo[i].split(":");
        if ( ipAddress[0] == "IP" ) return ipAddress[1];
    return false;
// console.log(myIP());


  country_name: "UNITED STATES",
  country_code: "US",
  city: "(Unknown city)",
  ip: ""

3. Three way

Try it!

Syntax : http://l2.io/ip
Diplay Client IP address
--> x.x.x.x

Syntax : 
Display Client IP address in your HTML page using javascript
--> document.write('x.x.x.x');

Syntax : 
Set javascript variable "myip" to client IP address
--> myip = "x.x.x.x";
Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

Free Guide:

How to Choose the Right Charting Library for Your Application

How do you make sure that the charting library you choose has everything you need? Sign up to receive this detailed guide from FusionCharts, which explores all the factors you need to consider before making the decision.

  • Franz

    Boy you a god

  • JC

    the Third Option is what I need. But I have to get the country name also>? Any suggestions

  • Tberry Apapa

    Good stuff mate… good stuff!

  • http://www.codesarena.com/ Anirudh Anand

    How can I modify this code so that I can pass a domain name to the PHP script which can return the host IP address instead of the client Ip address ?

  • Ratheesh K

    Hey this code just return gateway ip address not client ip. because i have tried from different machine it is return same IP

Ending Soon
Free SitePoint Premium

Get one free year of unlimited book and course downloads on SitePoint Premium!