Javascript - Simple - getting an Ip from a URL

2019-09-22 07:17发布

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

标签: javascript ip
2条回答
【Aperson】
2楼-- · 2019-09-22 07:49

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.

查看更多
男人必须洒脱
3楼-- · 2019-09-22 07:52

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.

查看更多
登录 后发表回答