Javascript - 简单 - 从 URL 获取 Ip
我只是想实现这样的目标:
function getIp() {
var urlenter = document.getElementById('io');
var IPOUT = urlenter.IpAdress !------HERE!!!!!!!!------!
location.href = IPOUT;
}
所以如果我在“io”中输入“facebook.com”,它会将 href 设置为 facebook.com 的 IP 地址
I am simply trying to achieve something like this:
function getIp() {
var urlenter = document.getElementById('io');
var IPOUT = urlenter.IpAdress !------HERE!!!!!!!!------!
location.href = IPOUT;
}
so if I typed 'facebook.com' in 'io' it would set the href to the ip adress of facebook.com
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JavaScript 没有任何主机名或 IP 地址的概念。您必须使用外部服务从主机名解析 IP 地址。
JavaScript doesn't have any concept of hostnames or IP addresses. You would have to use an external service to resolve an IP address from a hostname.
仅靠 JavaScript 是不可能实现这一点的。
IP 地址和主机名之间的解析是在 DNS 级别处理的,这早于 JavaScript 诞生。它只能知道所连接的主机,无论是通过实际主机名还是 IP 地址。
尽管如此,您仍然可以让 JavaScript 连接到外部服务(例如通过 Ajax 的服务器),并让它进行解析。
That's not possible with JavaScript alone.
The resolving between IP addresses and hostnames are handled at the DNS level, which is way before JavaScript ever comes to life. It only gets to know the host it is connected to, whether that is by an actual hostname or an IP address.
Still though, you could have JavaScript connect to an outside service, e.g. your server via Ajax, and have it make the resolving.