Javascript - Simple - getting an Ip from a URL

混江龙づ霸主 提交于 2019-12-25 18:59:25

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9054267/javascript-simple-getting-an-ip-from-a-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!