6

I've decided that there are some errors which I don't want to go to the browser error handler. But I still want to know about them. In my actual code I have a function which stores the errors in a hidden element and ajax submits them to a database. Following is a simplified version of my try block:

try
{
  newValueEvaled = eval(newValue);
}catch(err)
{
  alert("Error caught: Line " + err.lineNumber + ((err.columnNumber != undefined)?', Column:' + err.columnNumber:"") + '\n' + err.message);
}

I'd like the columnNumber too. Currently it is never there, but somehow the browser error console has access to it. Can anyone tell me how I can get access to it as well?

3
  • Erm, why do you want to do this? The error going to the browser allows the native browser, WebDeveloper extension, FireBug extension to show you the same information... often with more info, and without blocking the script. .lineNumber is a Firefox only extension btw. Commented May 5, 2011 at 18:34
  • @Rudu The idea is to produce a log of all javascript errors via a simple ajax request. Errors logs are only helpful if they are specific, so including data like this is the whole point. Commented May 26, 2011 at 21:50
  • Would be nice if this became standard developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 29, 2020 at 16:50

2 Answers 2

3

I'm almost certain it's not possible to get the error column number from JavaScript running in the page. Firebug/WebKit console/IE console has access to internal browser objects that provide more information about the call stack than is available to code running inside the page.

Sign up to request clarification or add additional context in comments.

2 Comments

According to the Mozilla Docuentation (developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…) The Error object does not contain any column information. However, I still think there has to be some way to make this data available to Javascript. Maybe an addon?
I imagine it's possible to write an add-on that would provide this. A quick googling suggests that it doesn't already exist. Might be a fun project?
3

You can access the error line and possibly column using a custom error handler function:

function dumpErrors(error, file, line, column)
{
    alert('Error: ' + error + ', occurred in file: ' + file + ', on line: ' + line + ', at column: ' + (column || 'unknown'));
}
onerror = dumpErrors;

The «line» is available for all browsers. For the «column», it seems it's available on latest Chrome (release 30.0+), but not on Firefox (release 17, running on my Linux).

1 Comment

Mobile Safari (at least in iOS 8) also supports column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.