Jump to content

How do I print JavaScript errors to the screen?


atrum

Recommended Posts

Hello all,

 

I have little experience with javascript, but I am trying to change that.

 

Today it dawned on me that learning how to script in javascript would be alot easier if I could see all the errors my code is putting out.

 

For example in PHP, if I wanted to see any errors that my code may produce, I would simply enter.

 

ini_set('display_errors','On');

 

How can I do that for javascript?

 

Thank you in advance for your help.

Do you think something like this would work?

 

onerror=handleErr
var txt=""

function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n"
txt+="Error: " + msg + "\n"
txt+="URL: " + url + "\n"
txt+="Line: " + l + "\n\n"
txt+="Click OK to continue.\n\n"
alert(txt)
return true
} 

 

I dont really like that it comes up in a popup box. Is there a way to make this just write the error to the screen?

onerror=handleErr;
var txt="";

function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n";
txt+="Error: " + msg + "\n";
txt+="URL: " + url + "\n";
txt+="Line: " + l + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
return true;
} 

 

should use semicolons after your variables there ;)

 

also i hope you defined handleERR before you output it might have problems if you do not  ;)

 

also should never use document.write or alert to output stuff. as its too global.

call errors like this:

var div = document.createElement('div');
div.className = 'foo';
div.appendChild(document.createTextNode('this is an error'));
document.body.appendChild(div);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.