Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get client IP address in JavaScript?

Tags:

javascript

I'm trying to get the client's IP address. This is my code so far. Any suggestions?

<script type="application/javascript">
  function getIP(json) {
    document.write("My public IP address is: ", json.ip);
  }
</script>
<script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>
like image 417
Naam Avatar asked Jan 18 '17 05:01

Naam


2 Answers

Try This.Little bit Help to you.

<script type="application/javascript">
  function getIP(json) {
    document.write("My public IP address is: ", json.ip);
  }
</script>

<script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>
like image 106
Sanjay Avatar answered Oct 06 '22 04:10

Sanjay


function user_location() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      console.log( this.responseText);
    }
  };
  xhttp.open("GET", "//api.ipify.org?format=json", true);
  xhttp.send();
}
like image 36
Kaushik Makwana Avatar answered Oct 06 '22 06:10

Kaushik Makwana