Reusable error reporting snippet to report browser errors
Place code at end of body tag.
(function() {
//Using jQuery for these AJAX requests, loading if it's not already loaded
jQuery == undefined ? getJQuery(errorReporting) : errorReporting();
//Load jQuery asynchronously with a callback when ready
function getJQuery(success) {
var script = document.createElement('script');
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js';
var head = document.getElementsByTagName('head')[0], done = false;
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == 'loaded'
|| this.readyState == 'complete')) {
done = true;
success();
script.onload = script.onreadystatechange = null;
}
};
head.appendChild(script);
}
function errorReporting() {
//The 2po.st link I use for client-side Javascript error reporting
var postLink = 'c-K7RveyY4iN';
var sendErrorReport = function(msg, url, line) {
jQuery.ajax({
type: 'POST',
url: 'https://2po.st/'+postLink,
data: {msg: msg, url: url, line: line}, //Pass data to 2po.st
dataType: 'jsonp'
//Could add AJAX response handlers here, no need for now
});
};
window.onerror = function (msg, url, line) {
sendErrorReport(msg,url,line);
return true; // Same as preventDefault
};
window.addEventListener('error', function (e) {
sendErrorReport(e.message, e.filename, e.lineno);
e.preventDefault();
});
//Add try-catch blocks or other handlers to report errors not caught by the error event
}
})();
Back Home