Code Self Study Forum

Report Javascript Errors

I came up with this script that will catch errors in the console of a website. You can also call the error report function with try/catch in any function.

window.onerror = function (msg, url, lineNo, columnNo, error) {
return false;

function error_report(error) {
//Call AJAX function
if (window.XMLHttpRequest) {var xmlhttp = new XMLHttpRequest();} 
else {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {}};
var uid=localStorage.getItem("KDI19842011QrwtA");
if (uid == null) {uid='blank';}
var url=window.location.href;"GET","https://""/ajax/error-report.php?id="+url+"&uid="+uid+"&err="+error,true);


The code will record the page, error message, and user id if the user is signed in. This can be useful if you have users so you can respond to direct messages on bug reports…

1 Like

I was wondering what this block is hoping to accomplish.

1 Like

Good catch. I was thinking to give the user an error message, but I will just kill that line for now…

1 Like

And it’s probably good to remind everyone that this code needs to begin everything or else it won’t be triggered.

Well, the first window.onerror function will trigger it when an error gets logged in the clients browser console… otherwise, yeah, you have to call it custom from whatever function…