tags:

views:

4199

answers:

5

I would like to use javascript to determine the IP address of a host, as seen from the clients computer. Is it possible?

A: 

I don't think this is allowed by most browsers for security reasons, in a pure JavaScript context as the question asks.

Nick Craver
A: 

If the client has Java installed, you could do something like this:

ipAddress = java.net.InetAddress.getLocalHost().getHostAddress();

Other than that, you will probably have to use a server side script.

Alex

Alex Fort
+2  A: 

There's no notion of hosts or ip-addresses in the javascript standard library. So you'll have to access some external service to look up hostnames for you.

I recommend hosting a cgi-bin which looks up the ip-address of a hostname and access that via javascript.

Hans Sjunnesson
cgi-bin? That's old school. I like it!
Andrew Hedges
+1  A: 

Doing this would require to break the browser sandbox. Try to let your server do the lookup and request that from the client side via XmlHttp.

Tomalak
+12  A: 

Edit: This question gave me an itch, so I put up a JSONP webservice on Google App Engine that returns the clients ip address. Usage:

<script type="application/javascript">
function getip(json){
  alert(json.ip); // alerts the ip address
}
</script>

<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"&gt; </script>

Yay, no server proxies needed.


Pure JS can't. If you have a server script under the same domain that prints it out you could send a XMLHttpRequest to read it.

Zach