I would use a web service that can return JSON (along with jQuery to make things simpler). Below are all the free active IP lookup services I could find and the information they return. If you know of any more, then please add a comment and I'll update this answer.


DB-IP

Try it: http://api.db-ip.com/addrinfo?api_key=<your api key>&addr=<ip address>

Returns:

{
  "address": "116.12.250.1",
  "country": "SG",
  "stateprov": "Central Singapore",
  "city": "Singapore"
}

Limitations:

  • 2,500 requests per day
  • Doesn't support JSONP callbacks
  • Requires IP address parameter
  • Requires an email address to get your API key
  • No SSL (https) with the free plan

Geobytes

Try it: http://gd.geobytes.com/GetCityDetails

$.getJSON('http://gd.geobytes.com/GetCityDetails?callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "geobytesforwarderfor": "",
  "geobytesremoteip": "116.12.250.1",
  "geobytesipaddress": "116.12.250.1",
  "geobytescertainty": "99",
  "geobytesinternet": "SA",
  "geobytescountry": "Saudi Arabia",
  "geobytesregionlocationcode": "SASH",
  "geobytesregion": "Ash Sharqiyah",
  "geobytescode": "SH",
  "geobyteslocationcode": "SASHJUBA",
  "geobytescity": "Jubail",
  "geobytescityid": "13793",
  "geobytesfqcn": "Jubail, SH, Saudi Arabia",
  "geobyteslatitude": "27.004999",
  "geobyteslongitude": "49.660999",
  "geobytescapital": "Riyadh ",
  "geobytestimezone": "+03:00",
  "geobytesnationalitysingular": "Saudi Arabian ",
  "geobytespopulation": "22757092",
  "geobytesnationalityplural": "Saudis",
  "geobytesmapreference": "Middle East ",
  "geobytescurrency": "Saudi Riyal",
  "geobytescurrencycode": "SAR",
  "geobytestitle": "Saudi Arabia"
}

Limitations:

  • 16,384 requests per hour
  • No SSL (https) with the free plan
  • Can return the wrong location (I'm in Singapore, not Saudi Arabia)

GeoIPLookup.io

Try it: https://json.geoiplookup.io/api

$.getJSON('https://json.geoiplookup.io/api?callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
    "ip": "116.12.250.1",
    "isp": "SGPOST",
    "org": "Singapore Post Ltd",
    "hostname": "116.12.250.1",
    "longitude": "103.807",
    "latitude": "1.29209",
    "postal_code": "",
    "city": "Singapore",
    "country_code": "SG",
    "country_name": "Singapore",
    "continent_code": "AS",
    "region": "Central Singapore",
    "district": "",
    "timezone_name": "Asia\/Singapore",
    "connection_type": "",
    "asn": "AS3758 SingNet",
    "currency_code": "SGD",
    "currency_name": "Singapore Dollar",
    "success": true
}

Limitations:

  • Unknown

geoPlugin

Try it: http://www.geoplugin.net/json.gp

$.getJSON('http://www.geoplugin.net/json.gp?jsoncallback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "geoplugin_request": "116.12.250.1",
  "geoplugin_status": 200,
  "geoplugin_credit": "Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\\'http://www.maxmind.com\\'>http://www.maxmind.com</a>.",
  "geoplugin_city": "Singapore",
  "geoplugin_region": "Singapore (general)",
  "geoplugin_areaCode": "0",
  "geoplugin_dmaCode": "0",
  "geoplugin_countryCode": "SG",
  "geoplugin_countryName": "Singapore",
  "geoplugin_continentCode": "AS",
  "geoplugin_latitude": "1.2931",
  "geoplugin_longitude": "103.855797",
  "geoplugin_regionCode": "00",
  "geoplugin_regionName": "Singapore (general)",
  "geoplugin_currencyCode": "SGD",
  "geoplugin_currencySymbol": "&#36;",
  "geoplugin_currencySymbol_UTF8": "$",
  "geoplugin_currencyConverter": 1.4239
}

Limitations:

  • 120 requests per minute
  • No SSL (https) with the free plan

Hacker Target

Try it: https://api.hackertarget.com/geoip/?q=<ip address>

Returns:

IP Address: 116.12.250.1
Country: SG
State: N/A
City: Singapore
Latitude: 1.293100
Longitude: 103.855797

Limitations:

  • 50 requests per day
  • Doesn't support JSONP callbacks
  • Requires IP address parameter
  • Returns plain text

ipapi.co

Try it: https://ipapi.co/json/

$.getJSON('https://ipapi.co/json/', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "ip": "116.12.250.1",
  "city": "Singapore",
  "region": "Central Singapore Community Development Council",
  "country": "SG",
  "country_name": "Singapore",
  "postal": null,
  "latitude": 1.2855,
  "longitude": 103.8565,
  "timezone": "Asia/Singapore"
}

Limitations:

  • 1,000 requests per day
  • Requires SSL (https)

IP-API.com

Try it: http://ip-api.com/json

$.getJSON('http://ip-api.com/json?callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "as": "AS3758 SingNet",
  "city": "Singapore",
  "country": "Singapore",
  "countryCode": "SG",
  "isp": "SingNet Pte Ltd",
  "lat": 1.2931,
  "lon": 103.8558,
  "org": "Singapore Telecommunications",
  "query": "116.12.250.1",
  "region": "01",
  "regionName": "Central Singapore Community Development Council",
  "status": "success",
  "timezone": "Asia/Singapore",
  "zip": ""
}

Limitations:

  • 150 requests per minute
  • No SSL (https) with the free plan

Ipdata.co

Try it: https://api.ipdata.co

$.getJSON('https://api.ipdata.co', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "ip": "116.12.250.1",
  "city": "Singapore",
  "region": "Central Singapore Community Development Council",
  "region_code": "01",
  "country_name": "Singapore",
  "country_code": "SG",
  "continent_name": "Asia",
  "continent_code": "AS",
  "latitude": 1.2931,
  "longitude": 103.8558,
  "asn": "AS3758",
  "organisation": "SingNet",
  "postal": "",
  "calling_code": "65",
  "flag": "https://ipdata.co/flags/sg.png",
  "emoji_flag": "\ud83c\uddf8\ud83c\uddec",
  "emoji_unicode": "U+1F1F8 U+1F1EC",
  "is_eu": false,
  "languages": [
    {
      "name": "English",
      "native": "English"
    },
    {
      "name": "Malay",
      "native": "Bahasa Melayu"
    },
    {
      "name": "Tamil",
      "native": "\u0ba4\u0bae\u0bbf\u0bb4\u0bcd"
    },
    {
      "name": "Chinese",
      "native": "\u4e2d\u6587"
    }
  ],
  "currency": {
    "name": "Singapore Dollar",
    "code": "SGD",
    "symbol": "S$",
    "native": "$",
    "plural": "Singapore dollars"
  },
  "time_zone": {
    "name": "Asia/Singapore",
    "abbr": "+08",
    "offset": "+0800",
    "is_dst": false,
    "current_time": "2018-05-09T12:28:49.183674+08:00"
  },
  "threat": {
    "is_tor": false,
    "is_proxy": false,
    "is_anonymous": false,
    "is_known_attacker": false,
    "is_known_abuser": false,
    "is_threat": false,
    "is_bogon": false
  }
}

Limitations:

  • 1,500 requests per day
  • Requires an email address to get your API key
  • Requires SSL (https)

IP Find

Try it: https://ipfind.co/me?auth=<your api key>

$.getJSON('https://ipfind.co/me?auth=<your_api_key>', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "ip_address": "116.12.250.1",
  "country": "Singapore",
  "country_code": "SG",
  "continent": "Asia",
  "continent_code": "AS",
  "city": "Singapore",
  "county": null,
  "region": "Central Singapore",
  "region_code": "01",
  "timezone": "Asia/Singapore",
  "owner": null,
  "longitude": 103.8565,
  "latitude": 1.2855,
  "currency": "SGD",
  "languages": [
    "cmn",
    "en-SG",
    "ms-SG",
    "ta-SG",
    "zh-SG"
  ]
}

Limitations:

  • 300 requests per day
  • Requires registration to get your API key

ipgeolocation

Try it: https://api.ipgeolocation.io/ipgeo?apiKey=<your api key>

$.getJSON('https://api.ipgeolocation.io/ipgeo?apiKey=<your_api_key>', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "ip": "116.12.250.1",
  "continent_code": "AS",
  "continent_name": "Asia",
  "country_code2": "SG",
  "country_code3": "SGP",
  "country_name": "Singapore",
  "country_capital": "Singapore",
  "state_prov": "Central Singapore",
  "district": "",
  "city": "Singapore",
  "zipcode": "",
  "latitude": "1.29209",
  "longitude": "103.807",
  "is_eu": false,
  "calling_code": "+65",
  "country_tld": ".sg",
  "languages": "cmn,en-SG,ms-SG,ta-SG,zh-SG",
  "country_flag": "https://ipgeolocation.io/static/flags/sg_64.png",
  "isp": "SGPOST",
  "connection_type": "",
  "organization": "Singapore Post Ltd",
  "geoname_id": "1880252",
  "currency": {
    "name": "Dollar",
    "code": "SGD"
  },
  "time_zone": {
    "name": "Asia/Singapore",
    "offset": 8,
    "is_dst": false,
    "current_time": "2018-06-12 09:06:49.028+0800"
  }
}

Limitations:

  • 50,000 requests per month
  • Requires registration to get your API key

ipify

Try it: https://api.ipify.org/?format=json

$.getJSON('https://api.ipify.org?format=jsonp&callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "ip": "116.12.250.1"
}

Limitations:

  • None

IPInfoDB

Try it: https://api.ipinfodb.com/v3/ip-city/?key=<your api key>&format=json

$.getJSON('https://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&format=json&callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "statusCode": "OK",
  "statusMessage": "",
  "ipAddress": "116.12.250.1",
  "countryCode": "SG",
  "countryName": "Singapore",
  "regionName": "Singapore",
  "cityName": "Singapore",
  "zipCode": "048941",
  "latitude": "1.28967",
  "longitude": "103.85",
  "timeZone": "+08:00"
}

Limitations:

  • Two requests per second
  • Requires registration to get your API key

ipinfo.io

Try it: https://ipinfo.io/json

$.getJSON('https://ipinfo.io/json', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "ip": "116.12.250.1",
  "hostname": "No Hostname",
  "city": "Singapore",
  "region": "Central Singapore Community Development Council",
  "country": "SG",
  "loc": "1.2931,103.8558",
  "org": "AS3758 SingNet"
}

Limitations:

  • 1,000 requests per day

ipstack (formerly freegeoip.net)

Try it: http://api.ipstack.com/<ip address>?access_key=<your api key>

$.getJSON('http://api.ipstack.com/<ip_address>?access_key=<your_api_key>', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
    "ip": "116.12.250.1",
    "type": "ipv4",
    "continent_code": "AS",
    "continent_name": "Asia",
    "country_code": "SG",
    "country_name": "Singapore",
    "region_code": "01",
    "region_name": "Central Singapore Community Development Council",
    "city": "Singapore",
    "zip": null,
    "latitude": 1.2931,
    "longitude": 103.8558,
    "location": {
        "geoname_id": 1880252,
        "capital": "Singapore",
        "languages": [{
            "code": "en",
            "name": "English",
            "native": "English"
        },
        {
            "code": "ms",
            "name": "Malay",
            "native": "Bahasa Melayu"
        },
        {
            "code": "ta",
            "name": "Tamil",
            "native": "\u0ba4\u0bae\u0bbf\u0bb4\u0bcd"
        },
        {
            "code": "zh",
            "name": "Chinese",
            "native": "\u4e2d\u6587"
        }],
        "country_flag": "http:\/\/assets.ipstack.com\/flags\/sg.svg",
        "country_flag_emoji": "\ud83c\uddf8\ud83c\uddec",
        "country_flag_emoji_unicode": "U+1F1F8 U+1F1EC",
        "calling_code": "65",
        "is_eu": false
    }
}

Limitations:

  • 10,000 requests per month
  • Requires IP address parameter
  • Requires registration to get your API key
  • No SSL (https) with the free plan

jsonip.com

Try it: https://jsonip.com

$.getJSON('https://jsonip.com/?callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "ip": "116.12.250.1",
  "about": "/about",
  "Pro!": "http://getjsonip.com",
  "reject-fascism": "Liberal America will prevail"
}

Limitations:

  • The response includes upsell and politics

JSON Test

Try it: http://ip.jsontest.com/

$.getJSON('http://ip.jsontest.com/?callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "ip": "116.12.250.1"
}

Limitations:

  • No SSL (https)
  • Goes down a lot (over quota), so I wouldn't use it for production
  • Returns IPv6 address if you have one, which may not be what you want

Nekudo

Try it: https://geoip.nekudo.com/api

$.getJSON('https://geoip.nekudo.com/api', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "city": "Singapore",
  "country": {
    "name": "Singapore",
    "code": "SG"
  },
  "location": {
    "accuracy_radius": 50,
    "latitude": 1.2855,
    "longitude": 103.8565,
    "time_zone": "Asia/Singapore"
  },
  "ip": "116.12.250.1"
}

Limitations:

  • Blocked by ad blockers using the EasyPrivacy list

Stupid Web Tools

Try it: http://www.stupidwebtools.com/api/my_ip.json

$.getJSON('http://www.stupidwebtools.com/api/my_ip.json', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Returns:

{
  "my_ip": {
    "ip": "116.12.250.1",
    "others": []
  }
}

Limitations:

  • No SSL (https)

Keep in mind that since these are all free services, your mileage may vary in terms of exceeding quota and uptime, and who knows when/if they will be taken offline down the road (exhibit A: Telize). Most of these services also offer a paid tier in case you want more features like SSL support.

Also, as skobaljic noted in the comments below, the request quotas are mostly academic since this is happening client-side and most end users will never exceed the quota.

UPDATES

怎样用JavaScript 程序获取客户的IP 地址? - 知乎

https://www.zhihu.com/question/20675353

可以利用WebRTC(Chrome/Firefox):. //创建RTCPeerConnection接口let conn = new RTCPeerConnection({ iceServers: [] }) let noop = function(){} ...

How to get client's IP address using JavaScript? - Stack Overflow

https://stackoverflow.com/questions/391979/how-to-get-clients-ip-address-using-javascript

<script type="text/javascript" src="userinfo.0.0.1.min.js"></script> UserInfo.getInfo( function(data) { alert ...

javascript如何通过访问者ip地址获取ip地址对应的物理地址API接口_ ...

https://blog.csdn.net/sd_tz_wzg/article/details/41825845

2014年12月9日 ... ... 如何通过访问者ip地址获取ip地址对应的物理地址API接口新浪的IP地址查询接口 :http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js ...

js获取访问者ip是什么-和js获取访问者ip相关的问题-阿里云开发者社区

http://developer.aliyun.com/askzt/10328161.html

阿里云开发者社区为开发者提供和js获取访问者ip相关的问题,如果您想了解js获取 访问者ip相关的问题,欢迎来阿里云开发者社区。阿里云开发者社区还有和云计算 ...

Get visitor IP address using Javascript

https://www.abstractapi.com/guides/get-ip-address-javascript

Nov 19, 2020 ... Retrieve the IP address of your website's visitors using a simple and easy-to- integrate javascript snippet. Step by step guide by Abstract.

Java获取访问者Ip并限制Ip访问页面_sunon_的博客-CSDN博客

https://blog.csdn.net/sunon_/article/details/105195809

2020年3月30日 ... 就是网站内的部分文章只有白名单内的ip才能打开。因为是静态化的网站,所有 文章都是静态html页面。所以首先想到的就是直接js获取访问者ip ...

js获取客户端真实IP - 简书

https://www.jianshu.com/p/956aeb07a9c3

2018年5月23日 ... 使用新浪接口:(我测试过,此方法好像不行。访问网站也不能访问了。) <script type="text/javascript ...

Java获取访问者Ip并限制Ip访问页面- osc_z7xhzbll的个人空间 ...

https://my.oschina.net/u/4315935/blog/4187634

2020年3月30日 ... 因为是静态化的网站,所有文章都是静态html页面。所以首先想到的就是直接js获取 访问者ip然后再判断是否在白名单内,不在白名单内就到没有权限 ...

How to get the client IP address with Javascript only | Our Code World

https://ourcodeworld.com/articles/read/257/how-to-get-the-client-ip-address-with-javascript-only

Sep 19, 2016 ... In this article you'll learn how to retrieve the user IP (private using pure javascript and public using a third party service) easily with a couple of ...

How to get client IP address using JavaScript ? - GeeksforGeeks

https://www.geeksforgeeks.org/how-to-get-client-ip-address-using-javascript/

Feb 3, 2020 ... These are the application services that fetch the user IP address and simply return it in three formats, plain text, JSON and JSONP format. There ...

js 返回访问者ip_rds内网ip_如何格式化js-华为云

https://www.huaweicloud.com/term/topic-642701.html

华为云为您介绍关于js 返回访问者ip相关的信息内容。同时为您提js 返回访问者ip供 相关的博客、视频、论坛相关内容,还有js 返回访问者ip开发者相关信息,更多 ...

Get Visitor IP Address with Google Tag Manager | Mixed Analytics

https://mixedanalytics.com/blog/visitor-ip-address-google-tag-manager/

Feb 25, 2021 ... There are many ways to do this, but I'll describe how to do it using JavaScript since it's a universal method that will work for nearly any setup. In ...

[Solved] How to get Client Ip Address and Client Machine Name ...

https://www.codeproject.com/Questions/589062/HowplustoplusgetplusClientplusIpplusAddressplusand

JavaScript. Copy Code. <script type="text/javascript"> window.onload = function () { var script = document.createElement("script"); script.type ...

How To Detect The Location of Your Website's Visitors Using ...

https://medium.com/better-programming/how-to-detect-the-location-of-your-websites-visitor-using-javascript-92f9e91c095f

To get a good understanding of how to use this services, read their documentation. I'll be using IP Geolocation API for my example. I could use Vanilla JavaScript, ...

Detect location and local timezone of users in JavaScript ...

https://blog.logrocket.com/detect-location-and-local-timezone-of-users-in-javascript-3d9523c011b9/

Nov 27, 2017 ... With a single query, you can tell which country a user is in and get their timezone. How convenient. Many times, the information provided by IP ...

How to Get Visitor's IP Address in Nginx and Express.js Application ...

https://mhagemann.medium.com/how-to-get-visitors-ip-address-in-nginx-and-express-js-application-5823959eb6cb

js Application. Activating the forwarding of IP addresses requires some configuration on your server. This tutorial shows you how to obtain a visitor's IP address in 3 ...