JavaScript Get IP Address Of Client
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Riya
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>
- <!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.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- Here we defined div element with id attribute ‘res’ for appending retrieved IP address on webpage.
- Then we imported open source jquery library support for access jquery methods and api in script.
- In script we defined get method with parameters of ‘ipinfo’ api, function with response as parameter.
- Within that we appending return client IP address on html page div element ‘res’ and ‘json’ refers format of retrieving ip address.
- With the help of statement ‘https://ipinfo.io’ we can make a communication with the ipify servers then retrieve any clients IP addresses.
- 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.