Javascript 如何通过 Jquery 或 JS 获取计算机名和 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44176623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Computer Name and IP address by Jquery or JS
提问by Video Chat Developer
How to get Computer Name and IP addressby jqueryor JS?
如何通过jquery或JS获取计算机名和 IP 地址?
回答by Shubham
I found this code snippet that actually worked for me
我发现这个代码片段实际上对我有用
var RTCPeerConnection = /*window.RTCPeerConnection ||*/
window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
if (RTCPeerConnection) (function () {
var rtc = new RTCPeerConnection({ iceServers: [] });
if (1 || window.mozRTCPeerConnection) {
rtc.createDataChannel('', { reliable: false });
};
rtc.onicecandidate = function (evt) {
if (evt.candidate)
grepSDP("a=" + evt.candidate.candidate);
};
rtc.createOffer(function (offerDesc) {
grepSDP(offerDesc.sdp);
rtc.setLocalDescription(offerDesc);
}, function (e) { console.warn("offer failed", e);
});
var addrs = Object.create(null);
addrs["0.0.0.0"] = false;
function updateDisplay(newAddr) {
if (newAddr in addrs) return;
else addrs[newAddr] = true;
var displayAddrs = Object.keys(addrs).filter(function(k) {
return addrs[k];
});
document.getElementById('list').textContent =
displayAddrs.join(" or perhaps ") || "n/a";
}
function grepSDP(sdp) {
var hosts = [];
sdp.split('\r\n').forEach(function (line) {
if (~line.indexOf("a=candidate")) {
var parts = line.split(' '),
addr = parts[4],
type = parts[7];
if (type === 'host') updateDisplay(addr);
} else if (~line.indexOf("c=")) {
var parts = line.split(' '),
addr = parts[2];
updateDisplay(addr);
}
});
}
})(); else
{
document.getElementById('list').innerHTML = "<code>ifconfig| grep inet | grep -v inet6 | cut -d\" \" -f2 | tail -n1</code>";
document.getElementById('list').nextSibling.textContent = "In Chrome and Firefox your IP should display automatically, by the power of WebRTCskull.";
}
<div id="list"></div>
And unlike other codes which mostly returns server IP address it return client ip address of machine.Refer this article https://www.c-sharpcorner.com/blogs/getting-client-ip-address-or-local-ip-address-in-javascript
与其他主要返回服务器 IP 地址的代码不同,它返回机器的客户端 IP 地址。参考这篇文章 https://www.c-sharpcorner.com/blogs/getting-client-ip-address-or-local-ip-address -in-javascript
回答by Asbar Ali
To get Computer Name and IP Address using JavaScript,
要使用 JavaScript 获取计算机名称和 IP 地址,
-- ComputerName
-- 计算机名
var network = new ActiveXObject('WScript.Network');
// Show a pop up if it works
alert(network.computerName);
-- IP Address
- IP地址
$.getJSON("http://jsonip.com/?callback=?", function (data) {
console.log(data);
alert(data.ip);
});
回答by Mansi Teharia
You can get IP address by using simple ajax request as:-
您可以通过使用简单的 ajax 请求来获取 IP 地址:-
let xhttp = new XMLHttpRequest();
xhttp.open("GET", "https://api.ipify.org/?format=json", true);
xhttp.send();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
let ip = JSON.parse(this.responseText).ip;
}
};
回答by MikePappa
Browser, Operating System, Screen Colors, Screen Resolution, Flash version, and Java Support should all be detectable from JavaScript (and maybe a few more). However, computer name is not possible
浏览器、操作系统、屏幕颜色、屏幕分辨率、Flash 版本和 Java 支持都应该可以从 JavaScript 中检测到(也许还有更多)。但是,计算机名称是不可能的
回答by Red Boy
If you need these details client side, you could use third party service like https://www.ipify.org/and make API call to get the details like IP.
如果您需要这些详细信息客户端,您可以使用第三方服务如https://www.ipify.org/并进行 API 调用以获取 IP 等详细信息。
For other details, use similar service/services.
有关其他详细信息,请使用类似的服务/服务。
But if you are looking this details server side, then it depends what programming language you have used, though on server side this information readily available in HTTP Headers.
但是,如果您正在查看服务器端的详细信息,那么这取决于您使用的编程语言,尽管在服务器端,此信息在 HTTP 标头中很容易获得。
Refer similar questions for server side details.
有关服务器端详细信息,请参阅类似问题。
How to get the exact client browser name and version in Spring MVC?
回答by Mehmet Top?u
for computer name
对于计算机名称
<script type="text/javascript">
var network = new ActiveXObject('WScript.Network');
alert(network.computerName);
</script>

