Inspired? No home

Javascript error logging with AJAX

You spent days and nights making your javascript perfect but still users complains about errors. There is a way of making the debugging process a little less painful by logging javascript errors.

Every javascript error can be trapped in the window.onerror event. We can return true or false so we can choose if the user shall see the normal javascript error dialog.

This script will, in the very unlikely event of a javascript error, gather information about the error and send a httprequest to a page which will log the javascript error to the database.

function doError(msg,url,ln) {
var strValues = "errMsg=" + escape(msg);    strValues += "&errLine=" + ln;    strValues += "&queryString=" + escape(location.search);    strValues += "&Url=" + escape(location.pathname);    strValues += "&HTTPRef=" + escape(document.referrer);

if (typeof XMLHttpRequest != "object") {
    function XMLHttpRequest() {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}
var objSave = new XMLHttpRequest();
objSave.open("GET", "/errorSave/?" + strValues, false);    objSave.send("");
}

try {    window.onerror = doError;}catch(er) {}
Written on 03 August 2005.
blog comments powered by Disqus