13

Is it possible to resolve a hostname using Javascript?

Here would be hypothetical code:

var hostname = "www.yahoo.com";
var ipAddress = DnsLookup(hostname);
console.log(ipAddress);

I am looking for that magic DnsLookup() function. :-)

Simon East
  • 55,742
  • 17
  • 139
  • 133
Amir
  • 5,996
  • 13
  • 48
  • 61
  • @PatrikAlienus: you didn't understand the question. As [x-nl said](http://stackoverflow.com/questions/7113072/resolve-a-hostname-with-javascript/31500047#comment36968223_7113122), your comment is "bollocks". – Dan Dascalescu Jul 19 '15 at 10:03
  • @DanDascalescu I guess the question I cite above is wrong. Having a checkmark and 277 upvotes... – InanisAtheos Jul 19 '15 at 18:45
  • 2
    @PatrikAlienus: the OP wants to resolve an *arbitrary hostname* to an IP. The answer you link to only gets *the hostname of the current page* from `window.location` and doesn't attempt to resolve it. – Dan Dascalescu Jul 19 '15 at 21:41
  • I would say that this question is almost a duplicate of http://stackoverflow.com/questions/7113072/perform-a-dns-lookup-to-resolve-a-hostname-to-an-ip-address-using-javascript although you haven't stated whether you were using client-side script or server-side code like with NodeJS. – Simon East Nov 11 '15 at 08:17
  • 1
    Possible duplicate of [Can I perform a DNS lookup (hostname to IP address) using client-side Javascript?](http://stackoverflow.com/questions/102605/can-i-perform-a-dns-lookup-hostname-to-ip-address-using-client-side-javascript) – cweiske Aug 04 '16 at 06:27

4 Answers4

4

While there is no standard DNS functionality in JavaScript, you can always call a 3rd party public API that does DNS resolution.

For example, Encloud provides such an API, and you can make an XMLHttpRequest for it:

var oReq = new XMLHttpRequest();
oReq.onload = function () {
  var response = JSON.parse(this.responseText);
  alert(JSON.stringify(response.dns_entries));
}  
oReq.open("get", "https://www.enclout.com/api/v1/dns/show.json?auth_token=rN4oqCyJz9v2RRNnQqkx&url=stackoverflow.com", true);
oReq.send();

Of course, you should get your own Auth token. Free Enclout accounts are limited to 6 request per minute.

If you just want the IP, make a GET request for http://api.konvert.me/forward-dns/yourdomain.com.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • 4
    This resolves the hostname on the server of Enclout, not on the machine where the javascript is running. Resulting IP could thus be different. – Arthur Dec 11 '15 at 16:22
4

There's a relatively new (2018) proposed Internet standard called DNS over HTTPS (also called DoH) that's been taking off. It allows you to send wireformat DNS queries over HTTPS to "DoH servers". The nice thing is, with DoH you get the whole DNS protocol on top of HTTPS. That means you can obtain a lot useful information.

That being said, there's an open source JavaScript library called dohjs that makes it pretty easy to do a DNS lookup in the browser. Here's a quick example code snippet:

const resolver = new doh.DohResolver('https://1.1.1.1/dns-query')
resolver.query('www.yahoo.com')
  .then(console.log)
  .catch(console.error);

Full disclosure: I'm a contributor to dohjs.

There are a lot of resources on cURL's DNS over HTTPS wiki page including a list of public DoH servers and a list of DoH tools (mainly server and client software).

Community
  • 1
  • 1
kimbo
  • 2,513
  • 1
  • 15
  • 24
0

You'll need to callback to server-side and resolve the value from there. There is no standard DNS lookup functionality available in Javascript.

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
-3

No - javascript is blocked from making cross domain requests. There are potentially some hacks out there that might be able to help you (this one looked kinda promising), but by default you can't do that.

You might be able to request something and make sure you get back a HTTP 200.

Tejs
  • 40,736
  • 10
  • 68
  • 86