Perform A Dns Lookup To Resolve A Hostname To An Ip Address Using Javascript
Solution 1:
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 = newXMLHttpRequest();
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.
Solution 2:
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).
Solution 3:
You'll need to callback to server-side and resolve the value from there. There is no standard DNS lookup functionality available in Javascript.
Solution 4:
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.
Post a Comment for "Perform A Dns Lookup To Resolve A Hostname To An Ip Address Using Javascript"