0
有沒有一種方法可以處理JS中使用事件監聽器的ReferenceError或TypeError錯誤?我知道的嘗試..趕上..但這不是我正在尋找。通過eventListeners在JS中捕獲錯誤
例如:我正在尋找像document.onRefernceError或類似的事件監聽器的方法。
有沒有一種方法可以處理JS中使用事件監聽器的ReferenceError或TypeError錯誤?我知道的嘗試..趕上..但這不是我正在尋找。通過eventListeners在JS中捕獲錯誤
例如:我正在尋找像document.onRefernceError或類似的事件監聽器的方法。
我假設你正在嘗試執行全局錯誤處理。對於錯誤,你無法真正打入一個型本身,但可以使用監聽所有錯誤:
window.onerror = function(message, source, lineno, colno, error) { ... }
對於元素特定的錯誤你有不同的函數簽名(「歷史原因」 - 每文檔)
element.onerror = function(event) { ... }
在這兩種情況下,你應該能夠檢查error
或event
一類instanceof
檢查。
//in case of global level checking
error instanceof ReferenceError //boolean
//in case of element level checking
event.type === <type you're looking for> //boolean
文檔 - https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
帶我稍微接近於解決的問題 –