How to get user IP via Javascript?

Asked

Viewed 20,664 times

8

I have three commands that together return the user’s IP. I wanted to turn them into a single command with the command return. Kind of:

return userip;

How do I do it? Follow the code:

<html>
  <body>
    <script type="text/javascript">
      var userip;
    </script>

    <script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script>

    <script type="text/javascript">
      document.write("Seu IP e:", userip);
    </script>
  </body>
</html>

Thank you for your attention.

  • What are the three commands?

3 answers

14


You can use a function

I tried so, but due to the time of the call against the time of the injection of the external script it is not possible to obtain so "synchronous":

function getIp()
{
   if (!document.body) {
      return false;
   }

   var d = document.createElement("script");
   d.src = "https://l2.io/ip.js?var=userip";
   document.body.appendChild(d);

   return window.userip || false;
}

To use just call something like:

document.write(getIp());

or

alert(getIp());

The best thing to do asynchronous (still helps to avoid the render lock):

function getIp(callback)
{
    function response(s)
    {
        callback(window.userip);

        s.onload = s.onerror = null;
        document.body.removeChild(s);
    }

    function trigger()
    {
        window.userip = false;

        var s = document.createElement("script");
        s.async = true;
        s.onload = function() {
            response(s);
        };
        s.onerror = function() {
            response(s);
        };

        s.src = "https://l2.io/ip.js?var=userip";
        document.body.appendChild(s);
    }

    if (/^(interactive|complete)$/i.test(document.readyState)) {
        trigger();
    } else {
        document.addEventListener('DOMContentLoaded', trigger);
    }
}

getIp(function (ip) {
    console.log(ip);
});

  • @rafaelfranco4 I’m trying to correct

  • Thank you for your attention!

  • @rafaelfranco4 the first does not work due to "Runtime", events work parallel, use the second please.

  • Good afternoon William. How do I use the second example?

  • @rafaelfranco4 with callback, like this getIp(function (ip) {&#xA; alert(ip);&#xA;});

  • Thank you so much for everything! It worked right!!!

  • 1

    scored there. I was trying to figure out how to do it! rsrsrs

  • @rafaelfranco4 Thank you!

Show 3 more comments

3

To get the IP of the javascript user is possible to use any site, I recommend using the api ipify.

https://www.ipify.org

Use the Jquery version code it is quite easy that normal javascript, put it in your head first:

 <script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>

Put this exactly into your script element to run it, do a small test first. requires an updated or old Jquery version.

$(function() {
 $.getJSON("https://api.ipify.org?format=jsonp&callback=?",
  function(json) {
   document.write("Meu IP público é: ", json.ip);
  }
 );
});

Feel free to test the script with what I put below. Remember, the api site is a little slow I think so... it may take about 15 seconds for the IP to be updated, obviously depends on your internet.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<html>
  <head>
    <script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
    <script type="application/javascript">
      $(function() {
        $.getJSON("https://api.ipify.org?format=jsonp&callback=?",
          function(json) {
            document.write("Meu IP público é: ", json.ip);
          }
        );
      });
    </script>
  </head>
  <body>
  </body>
</html>

2

I use this API here: http://meuip.com/api-webservice-meuip.php

Very simple, look at:

<script>
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET", 'http://meuip.com/api/meuip.php');
  xmlhttp.send();
  xmlhttp.onload = function(e) {
    alert("Seu IP é: "+xmlhttp.response);
  }
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.