4

I want to get the name of the computer or its IP address through javascript.

How can this be obtained?

Hemanshu Bhojak
  • 16,972
  • 16
  • 49
  • 64
  • 1
    possible duplicate of [Get Client IP using just Javascript?](http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript) – Greg Hewgill May 13 '10 at 08:11
  • @Greg (et. al.): The name part probably makes it distinct enough. Also, he says "through Javascript" but the poster in the other question very clearly says "using no server-side code" which this question leaves open. – T.J. Crowder May 13 '10 at 08:17

3 Answers3

4

As Imran and Jamie have said, you can't do it entirely on the client.

It's trivial to get the computer's apparent, public IP address — but only if you send a request to your server, either by using XmlHTTPRequest or by appending a script tag to the head section or similar. The server can respond to that request by echoing the IP from which the request apparently came. How you get that information depends on your server-side technology.

That'll get you the IP (using a server request), but I don't know of a way to get the computer name.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

It's not possible. JavaScript doesn't have access outside browser's sandbox.

Imran
  • 87,203
  • 23
  • 98
  • 131
-1

Javascript can't do it alone, you have to use JQuery for that below is the code snippet

<script>
$.getJSON("https://api.ipify.org?format=json", function (data) {
  console.log("Ip address ====>", data.ip);
})
</script>
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 20 '22 at 12:53