Search code examples
javascripthtmlasp-classictimeout

How can I catch a browser timeout and execute my own error message?


I have a web page (classic asp) with a link to a local IP address, as follows:

<a href="http://192.168.1.89">Link</a>

If the local IP address is unavailable the web browser eventually times out and displays its own error message.

What I want to do is: catch the timeout before the browser displays its default error page and display an error on the same web page, next to the "Link".

e.g. <a href="http://192.168.1.89">Well Pump</a><div id="timeoutmsg">offline</div>

I am guessing I need some JavaScript and the timeout function, but I don't know where to begin.


Solution

  • Found this awesome workaround using pure javascript, no JScript, no ajax, no external libraries.
    Works at treat:
    Just need to upload a "test.gif" file to the local site(s).

    var url = 'http://192.168.1.89';
    var img = new Image();
    img.src = url + '/test.gif';
    img.onload = function() {
        document.getElementById("msg").innerHTML = "";
        window.location.href = url;
    }
    img.onerror = function() {
        document.getElementById("msg").innerHTML = "offline";
    }