Make a javascript in html that returns my ip

0

Hi, I'm doing a small html with your javascript to show me my ip, the code is as follows:

<HTML>
  <HEAD>

<script type="text/javascript">
    (function(loading, success){
        var xhr = XMLHttpRequest !== undefined
                    ? new XMLHttpRequest()
                    : new ActiveXObject('Microsoft.XMLHTTP');

            loading.apply(null, []);
            xhr.open('get', 'https://api.ipify.org/?format=json', true);
            xhr.onreadystatechange = function()
            {
                if(xhr.readyState === 4)
                {
                    success.call(null, JSON.parse(xhr.responseText));
                }
            }
            xhr.send();
    }(function()
    {
        document.getElementById('my-ip').innerHTML = 'cargando ip...';
    }, function(response)
    {
        document.getElementById('my-ip').innerHTML = response.ip;
    }));
</script>
<script type="text/javascript" src="https://api.ipify.org/?format=jsonp&callback=get_ip"></script>
  </HEAD>
  <BODY>
  mi ip es: <strong id="my-ip"></strong>
  </BODY>
</HTML>

But it does not work, it returns the following:

However, in the example, it worked: link

How could I fix it?

    
asked by Sergio Ramos 25.02.2017 в 18:03
source

2 answers

1

Yes, what happens is that the script goes below the div # mi-ip

    
answered by 25.02.2017 / 18:17
source
1

  <HEAD>


<script type="text/javascript">
    (function(loading, success){
        var xhr = XMLHttpRequest !== undefined
                    ? new XMLHttpRequest()
                    : new ActiveXObject('Microsoft.XMLHTTP');

            loading.apply(null, []);
            xhr.open('get', 'https://api.ipify.org/?format=json', true);
            xhr.onreadystatechange = function()
            {
                if(xhr.readyState === 4)
                {
                    success.call(null, JSON.parse(xhr.responseText));
                }
            }
            xhr.send();
    }(function()
    {
        document.getElementById('my-ip').innerHTML = 'cargando ip...';
    }, function(response)
    {
        document.getElementById('my-ip').innerHTML = response.ip;
    }));
</script>
<script type="text/javascript" src="https://api.ipify.org/?format=jsonp&callback=get_ip"></script>
  </HEAD>
  <BODY>
  mi ip es: <strong id="my-ip"></strong>
  </BODY>
</HTML>
    
answered by 25.02.2017 в 18:34