admin管理员组

文章数量:1402331

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?

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?

Share Improve this question asked May 5, 2011 at 18:23 LukeLuke 4354 silver badges12 bronze badges 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. – Rudu 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. – Luke Commented May 26, 2011 at 21:50
  • Would be nice if this became standard developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Wesley Commented Sep 29, 2020 at 16:50
Add a ment  | 

2 Answers 2

Reset to default 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.

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

本文标签: script debuggingLine and column in Javascript Error event attributesStack Overflow