how to check if website is down javascript

Solutions on MaxInterview for how to check if website is down javascript by the best coders in the world

showing results for - "how to check if website is down javascript"
Darcy
28 Nov 2019
1isSiteOnline("http://www.facebook.com",function(found){
2    if(found) {
3        // site is online
4    }
5    else {
6        // site is offline (or favicon not found, or server is too slow)
7    }
8})
9
Noah
20 Apr 2020
1function isSiteOnline(url,callback) {
2    // try to load favicon
3    var timer = setTimeout(function(){
4        // timeout after 5 seconds
5        callback(false);
6    },5000)
7
8    var img = document.createElement("img");
9    img.onload = function() {
10        clearTimeout(timer);
11        callback(true);
12    }
13
14    img.onerror = function() {
15        clearTimeout(timer);
16        callback(false);
17    }
18
19    img.src = url+"/favicon.ico";
20}
21