Created
November 10, 2010 12:32
-
-
Save gnab/670789 to your computer and use it in GitHub Desktop.
Get error line number of evaluated code in Firefox and Chrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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