7

I just want to ask, is there a way on how to get ip addresses using javascript only? been searching for quite a while now and most of the results were I need to use api(s). I have used this webrtc and it works great but it's not working on IE, API is great, I've tested some and that works great in different browsers.

but I need to get the code itself from api, or is it possible to get/extract the code from api itself and make a specified file for the source so I won't rely on source from the internet?

I need the RAW file from api, because if ever the src of the api went down, my site will be affected too, so I want it to get and create a external source and include it on my site.

Milap
  • 6,915
  • 8
  • 26
  • 46
Bryan Rance
  • 141
  • 1
  • 1
  • 9
  • Contact the author of an API, and ask for the file. – Teemu Jan 04 '16 at 05:08
  • Possible duplicate of [Get client IP using just JavaScript?](http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript) See Malta's answer: http://stackoverflow.com/a/5239085/1291428 – Sebas Jan 04 '16 at 05:16
  • ^Teemu so to be exact there's no other way on how to get the raw script? ^Sebas I have tried it, and I'm wondering since all of them has source, where can I get the raw file of that sources. – Bryan Rance Jan 04 '16 at 06:42
  • Any API for this will use some server-side code, so no other way to get the full source code. – Teemu Jan 04 '16 at 09:42
  • okay, so I'll probably just have to create php file to get ip and get the it via javascript. – Bryan Rance Jan 06 '16 at 00:33

4 Answers4

19

Try 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 may help you.

Harsh Sanghani
  • 1,666
  • 1
  • 14
  • 32
  • its working both options, sorry to ask this(noob question) but if ever or if possible jsonip and ipinfo went down even for just a second, my website will be affected also, so is there a way I can see/view/get the source code that were used on those sites? – Bryan Rance Jan 04 '16 at 05:17
  • 1
    I used this and I was wondering if ever I can get/view the code inside api – Bryan Rance Jan 04 '16 at 05:19
  • No I think you cant find any way to get ip without call any server. – Harsh Sanghani Jan 04 '16 at 05:54
  • 1
    I have also tried a lot for these but not any way. :) – Harsh Sanghani Jan 04 '16 at 05:54
  • @HarshSanghani you have tried and not/none of them worked? :\ I tried to get ip via php file and call it with ajax, but still not sure if I'm gonna go with that. – Bryan Rance Jan 04 '16 at 06:36
  • @BryanRance Using php its always open option but you asked for pure javascript and for that non of option is available.. – Harsh Sanghani Jan 04 '16 at 10:45
  • okay, so I probably need to switch from javascript to php in order to make this work – Bryan Rance Jan 06 '16 at 00:32
2

I could be wrong, but I think you can only detect the IP serverside, so you'll have to do some kind of a get/post request.

The other answer shows a possible implementation of this.

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

Community
  • 1
  • 1
Anthony
  • 13,434
  • 14
  • 60
  • 80
0

You need to create script at backend of your site, that will be return IP, and execute it via ajax.

Or on the stage of generating page (at backend), you can detect IP, and put it to cookie, than read cookie from JS:

function getCookie(name) {
  var matches = document.cookie.match(new RegExp(
    "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
  ));
  return matches ? decodeURIComponent(matches[1]) : undefined;
}
gvozd1989
  • 300
  • 1
  • 16
  • actually I need the IP address and I will store it inside a cookie, thats why. I followed your suggestion with regards to get the ip via ajax, but I still dont know if I'm gonna stick with that, since what I really need is pure javascript only – Bryan Rance Jan 04 '16 at 06:39
0

If you want source data you can get it from MaxMind: http://dev.maxmind.com/geoip/ There is a free and a paid version. Most of the IP info providers uses that library.

If you only need the IP, you can create a script of your own. Just create a backend script something like with PHP and request it from JS. Example: http://php.about.com/od/learnphp/qt/record_user_ip.htm

Babar Al-Amin
  • 3,939
  • 1
  • 16
  • 20