Javascript - Simple - getting an Ip from a URL

2019-09-22 07:09发布

问题:

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.



标签: javascript ip