What is DevRel? | What is Developer Relations ?
A to Z Full Forms and Acronyms

How to get client IP address using JavaScript and jQuery

Here I will explain How to get client IP address using JavaScript and jQuery. I have to create jQuery Ajax call for finding IP address.

 Introduction:

Today in this article, I will explain How to get client’s IP address using JavaScript and jQuery. Javascript or jQuery is unable to find the IP address of client machine, however with help of jQuery we can create HTTP requests, and server-side languages are able to find your public IP address. So let's get started.

 

Using third-party API(Get public IP Address):

For finding client’s IP address you need any server-side languages means create service on your server or use third-party API.

So there are two types of connection available to get the IP address.

 

Insecure connection HTTP

If you want to find client’s information from a website without SSL certificate, you can rely on ipinfo.io with simple Ajax call.

Write following code:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title></title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <script>

        var ipinfo;

        $.getJSON("http://ipinfo.io", function (data) {

            $("#info").html("City: " + data.city + " ,County: " + data.country + " ,IP: " + data.ip + " ,Location: " + data.loc + " ,Organisation: " + data.org + " ,Postal Code: " + data.postal + " ,Region: " + data.region + "")

        })

    </script>

</head>

<body>

    <p>Client's information:</p>

    <p id="info"></p>

</body>

</html>

 

In above code, data object contains localization information for client’s machine.

 

Secure connections HTTPS

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

Write following code:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title></title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <script>

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

                $("#info").html("IP: " + data.ip + "");

            })

    </script>

</head>

<body>

    <p>Client's information:</p>

    <p id="info"></p>

</body>

</html>

In above code, data object contain information for IP address.

 

If you have your own server, create the private function that returns IP address and calls that service using jQuery Ajax call.

 

 Like this post? Don’t forget to share it!

A to Z Full Forms and Acronyms
Nitin Pandit

Nitin Pandit

With over 10 years of vast development experience with different technologies, Nitin Pandit is Microsoft certified Most Valued Professional (Microsoft MVP) with a rich skillset that includes developing and managing IT/Web-based applications in different technologies, such as – C#.NET, ADO.NET, LINQ to SQL, WCF, and ASP.NET 2.0/3.x/4.0, WCF, WPF, MVC 5.0 (Razor), and Silverlight, along with client-side programming techniques, like jQuery and AngularJS. Nitin possesses a Master’s degree in Computer Science and has been actively contributing to the development community for its betterment. He has written more than 100 blogs/articles and 3 eBooks on different technologies to help improve the knowledge of young technology professionals. He has trained more than one lakh students and professionals, as a speaker in workshops and AppFests, conducted in more than 25 universities in North India.

Related Article

Cookies.

By using this website, you automatically accept that we use cookies. What for?

Understood