Get ip address using javascript

I just want to ask if there is a way to get IP addresses using only javascript? searched for a long time, and most of the results I should use api (s). I used this webrtc and it works fine, but it doesn’t work on IE, the API is wonderful, I tested some and works fine in different browsers.

but do I need to get the code itself from the api, or can I get / extract the code from the api itself and make the specified file for the source, so I won’t rely on the source from the Internet?

I need a RAW file from api, because if the src from api were down, it would affect my site too, so I want it to receive and create an external source and include it on my site.

+5
source share
4 answers

Try the following solution: -

First option: -

$(document).ready(function () { $.getJSON("http://jsonip.com/?callback=?", function (data) { console.log(data); alert(data.ip); }); }); 

Second option: -

 $.get("http://ipinfo.io", function(response) { alert(response.ip); }, "jsonp"); 

It can help you.

+14
source

I may be wrong, but I think that you can only detect IP servers, so you will have to make some kind of request to receive / send.

Another answer shows a possible implementation of this.

Also see this question: How to get client IP address only using javascript?

+2
source

You need to create a script on the server of your site that will return IP, and execute it through ajax.

Or at the page generation stage (on the backend), you can detect the IP and put it in a cookie, than read a cookie from JS:

 function getCookie(name) { var matches = document.cookie.match(new RegExp( "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" )); return matches ? decodeURIComponent(matches[1]) : undefined; } 
0
source

If you need the source data, you can get it from MaxMind: http://dev.maxmind.com/geoip/ There is a free and paid version. Most IP information providers use this library.

If you only need an IP address, you can create a script yourself. Just create a backend script something like PHP and ask JS for it. Example: http://php.about.com/od/learnphp/qt/record_user_ip.htm

0
source

Source: https://habr.com/ru/post/1239759/


All Articles