How to get the user's IP via Javascript?

5

I have three commands that together return the user's IP. I wanted to turn them into a single command with the command return . Like this:

return userip;

How do I? Here is the code:

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

    <script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script><scripttype="text/javascript">
      document.write("Seu IP e:", userip);
    </script>
  </body>
</html>

Thanks in advance for your attention.

    
asked by anonymous 07.12.2016 / 14:56

3 answers

7

You can use a function

I have tried this, but due to the time of the call against the injection time of the external script it is not possible to get "synchronous" mode:

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());

It's best to do asynchronous (so it also helps avoid render locking >):

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);
});
    
07.12.2016 / 15:02
1

I use this API here: link

Very simple, see:

<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>
    
30.11.2017 / 21:22
1

To get the IP of the javascript user it is possible to use some website, I recommend using ipify's api .

  

link

Use the JQuery version code it's pretty easy that normal javascript, put this in your head first:

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

Putexactlythisinyourscriptelementtorunit,doalittletestfirst.requiresanupdatedoroldversionofJQuery.

$(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 website is a little slow I think so ... it may take about 15 seconds for the IP to be updated, obviously it depends on your internet.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><html><head><scripttype="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script><scripttype="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>
    
01.12.2017 / 01:57