Skip to content

Instantly share code, notes, and snippets.

@gnab
Created November 10, 2010 12:32
Show Gist options
  • Save gnab/670789 to your computer and use it in GitHub Desktop.
Save gnab/670789 to your computer and use it in GitHub Desktop.
Get error line number of evaluated code in Firefox and Chrome
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
<body>
<script language="javascript">
var code = 'var a = c * 3;\nvar b = a + c;';
var script = $('<script />');
script.append('try { ' + code + ' } catch (e) { handleError(e); }');
$(document).append(script);
function handleError(e) {
var lineNumber = undefined;
if (e.lineNumber !== undefined) {
lineNumber = e.lineNumber + 1;
}
else {
parts = e.stack.split('\n')[1].split(':');
lineNumberStr = parts[parts.length - 2];
lineNumber = parseInt(lineNumberStr, 10);
}
alert('Error at line: ' + lineNumber);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment