I am currently developing a google chrome extension that gathers information about webpages that the user visits. Currently, my plugin is able to handle 2xx, 3xx, 4xx, and 5xx status codes. However, I also need to detect when a website does not exist, and I get the error code ERR_NAME_NOT_RESOLVED
. How do I do this in javascript? It seems as though my XMLHttpRequest
cannot even be triggered on sites that don't exist. How do I fix this?

- 10,703
- 2
- 31
- 47

- 362
- 1
- 7
- 17
-
`try{x=new XMLHttpRequest();x.open("http://dvcsdfsd.com")}catch(y){alert(y)}` works... – dandavis Jul 05 '16 at 19:51
-
If a site doesn't exist, shouldn't it be returning 404? Could it be a [DNS issue](http://superuser.com/questions/719559/why-cant-chrome-load-a-web-page-err-name-not-resolved)? – neilsimp1 Jul 05 '16 at 19:52
-
@neilsimp1 i can handle 404s, I'm talking about DNS issues. – Cameron Payton Jul 05 '16 at 19:52
-
@neilsimp1: if a site doesn't exist, how can it return a 404 (or anything else)? – dandavis Jul 05 '16 at 19:52
-
@dandavis please don't leave an answer as a comment, post an actual answer to the question. And if you could elaborate a little bit, that would also be great. Thanks – Cameron Payton Jul 05 '16 at 19:53
-
@neilsimp1 It doesn't; you just arrive at a blank page that says "lakjsbdlaj.com's server DNS address could not be found". I'm trying to detect when this happens – Cameron Payton Jul 05 '16 at 19:54
-
Possible duplicate of [How to detect DNS lookup error in JavaScript](http://stackoverflow.com/questions/9664097/how-to-detect-dns-lookup-error-in-javascript) – Haibara Ai Jul 06 '16 at 00:17
-
@dandavis Actually, that doesn't work. It doesn't throw an error the same way other things do, it still shows up exactly as he describes in the console even when placed inside a try-catch. – Darren S May 16 '21 at 03:33
-
@DarrenStults: I swear it worked 5 years ago, but thank for the update; useful. – dandavis May 16 '21 at 05:28
-
@dandavis Yeah it didn't make any sense to me when I stumbled across the same issue either, my best guess is maybe that it's because the xhrs are forced to be asynchronous now and the actual error throwing occurs on a different thread/task. – Darren S May 16 '21 at 08:26
2 Answers
TL;DR: xhr.onerror
should be what you're looking for, with a caveat.
So, the error you get you can get that when your computer has no connection, and I think if you can't connect to your device's DNS server. It's a failure of trying to initiate the XHR basically.
It would appear that the display of the error in the console might not be preventable by design, if you want to try to catch it you can use the .onerror
of your xhr. Try-catch doesn't seem to work for me, despite it seemingly like it should -- might have to do with its asynchronous nature? Not sure.
Also keep in mind that .onload
runs even if there's an error, even though it may not seem like it due to the lack of a connection existing.
If you have a connection to your DNS server and the resolved site simply doesn't exist, your xhr state should be 404 and this error should not appear, while if you don't have a connection/can't connect to the DNS server the xhr state should be 0 and the error should appear.
Sample code:
// you can assign a function because javascript
// in this example, we are using an anonymous function
const xhr = new XMLHttpRequest();
xhr.open('POST', '/');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// error handler example
xhr.onerror = () => {
alert('error');
console.log(xhr);
};
// needs to call as anonymous function to work right
xhr.onload = () => { handleResponse(xhr); };
xhr.send('example.com/action?input=yes');
As I was mentioning above, because both .onload
and .onerror
are run (in that order), I prefer to just make my onload function handle both errors and successes, hopefully this should work for you.
const handleResponse = (xhr) => {
if (xhr.state === 200) {
alert('you win');
} else {
alert('error');
}
};

- 516
- 4
- 8
XMLHttpRequest's onreadystatechange listener is fired 2+ times here for existing sites and only one time with status
property equal to 0
for non-existent sites, so this code works for me:
function xhrGetStatus(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('HEAD', url);
xhr.onreadystatechange = function(e) {
xhr.onreadystatechange = null;
callback({status: xhr.status, statusText: xhr.statusText});
};
xhr.send();
}
xhrGetStatus('http://abc123.com', function(r) {
console.log(r.status ? r.status : 'No such site?');
});

- 65,848
- 11
- 132
- 136
-
1status can be zero when the CORS header is not present in the server response so using a status of 0 to detect this is not reliable. – jpro Sep 26 '17 at 19:37