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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.