How to get client's IP address using javascript or jQuery


Hello, I would like to know, how to get client’s IP address using JavaScript and jQuery?any working example or code? thanks


Asked by:- jon
0
: 3292 At:- 3/14/2018 7:34:15 PM
javascript jQuery get client's IP address using javascript






2 Answers
profileImage Answered by:- jaiprakash

You can use the api ipinfo api to know the user's ip address, here is the demo code for it

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

it gives results

{
  "ip": "182.64.167.193",
  "city": "New Delhi",
  "region": "National Capital Territory of Delhi",
  "country": "IN",
  "loc": "28.6000,77.2000",
  "org": "Bharti Airtel Ltd., Telemedia Services"
}

you can read more details on https://ipinfo.io/developers

Try: ipinfo.io/json

To find the client’s information from a website with SSL certificate. You can access ipify service with simple Ajax call.

 $.getJSON("https://api.ipify.org?format=json", function (data) {

               alert(IP: " + data.ip + ");

   })

Using javascript:

window.onload = function () {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "https://api.ipify.org?format=jsonp&callback=ShowIP";
        document.getElementsByTagName("head")[0].appendChild(script);
    };
    function ShowIP(response) {
        document.getElementById("ipaddress").innerHTML = "Current IP Address is " + response.ip;
    } 
2
At:- 3/15/2018 11:55:08 AM
using ipinfo works for me, thanks 0
By : jon - at :- 4/2/2018 9:30:13 AM


profileImage Answered by:- bhanu

Above answer works, but there are many more other API's which can return client's public IP using Javascript.

JSONTest (Free)

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

IPregistry (100,000 Lookups are fre initally and then paid)

$.getJSON('https://api.ipregistry.co/?key=tryout', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Ipify (Open Source)

Javascript code

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

C# code

var httpClient = new HttpClient();
var ip = await httpClient.GetStringAsync("https://api.ipify.org");
Console.WriteLine($"My public IP address is: {ip}");

IP-API (Free, no API key required)

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

Geoplugin

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

There are many other which requires API key

https://ipdata.co/

https://ipfind.com/

https://ipgeolocation.io/

https://ipinfodb.com/

https://ipstack.com/

You can use any of the above services, but all few limitation like No SSL support or limited free requests.

I will recommend to use https://www.ipify.org/ which is free and open source.

0
At:- 5/25/2021 12:03:45 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly