views:

16409

answers:

8

I need to somehow pull the clients IP address using pure javascript, no server side code, not even SSI.

Any ideas?

I'm not against using a free 3rd party script, if someone can suggest one. This is an emergency stop gap until we can deploy new code.

+9  A: 

You can't. And even if you could, it would be the address of the machine, which is useless if they're communicating via a proxy or NAT. Find a way to get it server-side.

Shog9
A: 

Not possible in general unless you use some kind of external service.

Eugene Lazutkin
A: 

Even if you have java available to your JS, the following returns 127.0.0.1 for me, so I'd venture to say "No" as well:

alert(java.net.InetAddress.getLocalHost().getHostAddress());
Adam Bellaire
Not that I understand what you're doing here, but couldn't that be because you're doing `getLocalHost()`?
Deniz Dogan
@blahbah: Well, `getHostAddress()` is an instance method, so we need to have an `InetAddress` object first. One way to get it is `getLocalHost()`, another would be `getByName("localhost")`. So, on the surface, yes, it is because of that, but the issue is that we don't have a good way of generating an `InetAddress` for the local machine besides the loopback address.
Adam Bellaire
+1  A: 

There isn't really a way.

This goes through some of the possibilities. The code that uses Java will break if the user has multiple interfaces.

http://nanoagent.blogspot.com/2006/09/how-to-find-evaluate-remoteaddrclients.html

sjbotha
A: 

I'm no javascript guru, but if its possible you could open an iframe with http://www.whatismyip.com/automation/n09230945.asp as the source and read the content of the frame.

Edit: this wont work because of the cross domain security.

Unkwntech
May want to try this before accepting. I thought this would result in an "access denied" error due to violating cross-domain scripting rules.
Triptych
Not in an iframe. The problem is that you will have to pass the variables back and forth between the iframe and your main document. The IFrame won't be able to get an info from the parent document so make sure you are driving it the other way around.
Jason Jackson
A: 

Well, I am digressing from the question, but I had a similar need today and though I couldn't find the ID from the client using Javascript, I did the following.

On the server side: -

<div style="display:none;visibility:hidden" id="uip"><%= Request.UserHostAddress %></div>

Using Javascript

var ip = $get("uip").innerHTML;

I am using ASP.Net Ajax, but you can use getElementById instead of $get().

What's happening is, I've got a hidden div element on the page with the user's IP rendered from the server. Than in Javascript I just load that value.

This might be helpful to some people with a similar requirement like yours (like me while I hadn't figure this out).

Cheers!

Cyril Gupta
+6  A: 

You can, relaying it via server side with JSONP

And while googling to find one, found it here on SO http://stackoverflow.com/questions/102605/can-i-lookup-the-ip-address-of-a-hostname-from-javascript

<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;&lt;/script&gt;
Chad Grant
A: 

The last one is jsonpadding tecnique, and is the only possible actually.

carmichael84