3

I got this question in my interview and I have no Idea how to proceed. I have search net but not got any satisfactory answer. The question is:-

Write a Javascript that gives a pop-up alert shows the IP address of the user, and outputs a file as follows in the pop-up alert: My IP address: 10.123.123.43

Kindly help me out. Thanks

user3126444
  • 43
  • 1
  • 5
  • 1
    first result on google for "JavaScript IP address" is http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript which is a duplicate of your question. This proves to me that you indeed did not search the net as you say. – Darko Jan 07 '14 at 14:44

1 Answers1

1

You may want to read this : How to get client's IP address using javascript only?

function myIP() {
    if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
    else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.open("GET","http://api.hostip.info/get_html.php",false);
    xmlhttp.send();

    hostipInfo = xmlhttp.responseText.split("\n");

    for (i=0; hostipInfo.length >= i; i++) {
        ipAddress = hostipInfo[i].split(":");
        if ( ipAddress[0] == "IP" ) return ipAddress[1];
    }

    return false;
}
Community
  • 1
  • 1
Allison
  • 88
  • 1
  • 7