How can I handle errors in loading an iframe?

To detect whether your server is down or not, you can include an empty script file from your own domain. When the server is down, the onerror event handler will fire:

var el = document.createElement('script');
el.onerror = errorFunction;
el.src = "https://stackoverflow.com/questions/3705083/somebogusscript.js?" + new Date().getTime();
document.body.appendChild(el);

Note: don’t forget to add a random string to the src attribute to avoid the client using a cached version (which could stop a look at the server at all).

Leave a Comment