Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ip address using javascript

Tags:

javascript

api

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.

like image 719
Bryan Rance Avatar asked Jan 04 '16 05:01

Bryan Rance


2 Answers

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.

like image 172
Harsh Sanghani Avatar answered Oct 08 '22 15:10

Harsh Sanghani


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?

like image 44
Anthony Avatar answered Oct 08 '22 16:10

Anthony