明白了。添加代碼周圍的一個嘗試捕捉的偉大工程
catchableScript = function(scriptText) {
var scriptText = "try {\n"+scriptText+"\n} \n catch(ex){console.log(ex.lineNumber-1)}";
var script = document.createElement("script");
script.innerHTML = scriptText;
document.head.appendChild(script)
}
catchableScript("\n\n\n\n\n\n\nfoo.bar");
輸出:8
而且,除了有這是有用的信息,但是這是問題的答案。
編輯:
對於那些誰想要它,添加以下代碼:
window.CURRENTLY_EVALED_SCRIPT = scriptText;
var scriptText = "try {\n"+scriptText
+"\n} catch(ex){\n"
+"var lineNumber = ex.lineNumber-1;\n"
+"var errorOut = {};\n"
+"errorOut.exception = ex;\n"
+"errorOut.lines = window.CURRENTLY_EVALED_SCRIPT.split('\\n');\n"
+"var line = errorOut.lines[lineNumber-1];\n"
+"console.log(ex.message+' on line\\n'+line+' :: '+lineNumber, window.CURRENTLY_EVALED_SCRIPT_ELEMENT, errorOut);\n"
+"}";
...
window.CURRENTLY_EVALED_SCRIPT_ELEMENT = script
將導致控制檯輸出:
foo is not defined on line
foo.bar :: 8, <script>, Object { exception: ReferenceError, lines: Array[8] }
你也可以點擊<script>
去添加元素或lines
以查看所有行的數組。
工程就像一個魅力。
http://meta.stackoverflow.com/questions/336082/the-searchbar-works-better-with-tags-than-questions-explaining-this-could-reduc?noredirect=1#comment403450_336082 –
東西出現了在邊欄http://stackoverflow.com/questions/20838067/catch-js-errors-when-adding-a-script-dynamically?rq=1 –
先前的問題啓發了這一答案,但它不真的顯示它。 –