6

I was wondering is there a way to detect DNS lookup error in JavaScript.

is there some code or some trick work ?

or this is a mission impossible?

can somebody shed light on this? thanks a lot!

user1108486
  • 113
  • 3

2 Answers2

1

It's not possible to accurately predict what behavior will occurr when there's a 'DNS Lookup failure' on a random end-user's machine.

For example, many ISPs return their own 'helper' search pages/sites when a lookup is performed on a non-existant domain (NXDOMAIN). This is also known as DNS Hijacking.

A user may also configure their own domain entries on a local 'hostfile', which would give a 'false positive'.

For a more reliable method, you should perform this lookup on your server, which should give a better indicator as to whether the domain is reachable to the wider world. It will also let you differentiate between the site is merely being down or unreachable from the user's machine, instead of an invalid entry.

0

If you mean the err_name_not_resolved, that's a product of simply not having a connection or not being able to contact the dns server. If you're using an XHR then just simply assign a function to your xhr.onerror: xhr.onerror = () => { alert('Failed DNS resolution'); }; Note the error will still appear in the console and this is intentional and by design. The xhr.onload should also still attempt to run if it is not undefined as well, so that is something else to keep in mind.

If you mean finding a page but it doesn't exist, that should simply return a 404. With an xhr you just read the if(xhr.status === 404) { ... }.

If you mean finding a page but getting the wrong IP address, attempting phishing sites from dns hijacking, there are a few ways around that. The first is hard-coding the IP addresses, the second is certificates, a third is any sort of hidden code that both computers should know what to expect to verify one another. There are probably other ways too, that's just off the top of my head.