Javascript is unable to get the client IP, however, it is possible to retrieve it by making an ajax call to a service or web page that shows it to us.
dotmaui.com offers a free service with no limitations for this purpose. Just open this web page:
https://dotmaui.com/my-ip/raw/
Here is an example in pure Javascript:
function getIpAddress() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
alert("The IP address is: " + xmlhttp.responseText);
}
}
};
xmlhttp.open("GET", "https://dotmaui.com/my-ip/raw/", true);
xmlhttp.send();
}
Or with jQuery:
function getIpAddress() {
$.ajax({
url: 'https://dotmaui.com/my-ip/raw/',
success: function(data) {
alert("The IP address is: " + data);
}
});
}