All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Get IP Address Of Client

Last Updated : Mar 11, 2024

JavaScript Get IP Address Of Client

In this tutorial we will show you the solution of JavaScript get IP address of client, here we need to retrieve clients ip address so we need ipinfo api it will communicate with ipify servers for collect ip addresses.

Now we have to do one thing is import external jquery library support because for retrieve we are approaching jquery codes so finally we will achieve result of clients IP address too quickly.

Step By Step Guide On JavaScript Get IP Address Of Client :-

Here we defined div element with id ‘res’ for appending result of client IP address on webpage.

We imported open source jquery library support because we are retrieving using jquery.

In script using get method we passing ‘ipinfo’ api for access IP address by communicate with ipify servers then we collected IP address stored on response parameter finally we appended result on html page by id ‘res’ in json format.

<!DOCTYPE html>
<html>
    <head>
        <title>GET CLIENT IP</title>
    </head>
    <body>
        <div id="res"></div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script>
            $.get("https://ipinfo.io", function(response){
                $("#res").html(response.ip);
            },"json")
        </script>
</body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined div element with id attribute ‘res’ for appending retrieved IP address on webpage.
  7. Then we imported open source jquery library support for access jquery methods and api in script.
  8. In script we defined get method with parameters of ‘ipinfo’ api, function with response as parameter.
  9. Within that we appending return client IP address on html page div element ‘res’ and ‘json’ refers format of retrieving ip address.
  10. With the help of statement ‘https://ipinfo.io’ we can make a communication with the ipify servers then retrieve any clients IP addresses.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get clients IP address using javascript.

When we executes program on browser we need to confirm one thing is internet connection because we imported open sources jquery library supports otherwise we get error and also it can’t retrieve any IP addresses.

Now we can see the retrieved clients IP address and it will work on each different clients when using this concept.

I hope this tutorial on JavaScript get IP address of client helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪