簡體   English   中英

可以設置發生錯誤時顯示的嵌入式腳本標記的“ fileName”嗎?

[英]Possible to set the “fileName” of an inline script tag that is displayed when errors occur?

如果我有一個內聯腳本,例如以下未定義函數foo的內容,我希望能夠執行以下操作:

<script fileName='foo.js'>
  foo();
</script>

和控制在錯誤控制台中顯示什么文件名和/或控制在處理錯誤時將什么文件名傳遞給window.onerror。 例如,我想控制發送到的網址參數

window.onerror = function (errMsg, url, line) {
    console.log(url); //would like this to output 'foo.js'
    console.log(event.filename);  //would like this to also output 'foo.js'
}

默認情況下,除非腳本鏈接到外部文件,否則僅列出父html文件,但是能夠在不同的腳本標簽中包含內聯代碼以聲明錯誤源於哪個腳本標簽將非常好。

有什么辦法嗎?

據我所知,javascript不會在其中運行HTML標記。 這樣的事情對您有用嗎?

<script>
  try {
    foo();
    // the rest of the code...
  } catch (err) {
    console.log('Caught error in foo.js');
    console.error(err);
  }
</script>

如果將這些腳本動態添加到頁面,則應該可以使用此try / catch代碼將它們包裝起來並獲得所需的效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM