How to get computer name or IP from javascript?
I want to get 开发者_如何学运维the name of the computer or its IP address through javascript.
How can this be obtained?
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.
It's not possible. JavaScript doesn't have access outside browser's sandbox.
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>
精彩评论