Window: error イベント
error イベントは、リソースの読み取りに失敗したり、使用できなかったりした場合 — 例えば、スクリプトに実行エラーがあった場合に Window オブジェクトに発生します。
| バブリング | なし |
|---|---|
| キャンセル | 不可 |
| インターフェイス | Event または UIEvent |
| イベントハンドラープロパティ | onerror (en-US) |
イベントオブジェクトは、ユーザーインターフェイス要素から生成された場合は UIEvent のインスタンスとなり、それ以外の場合は Event となります。
例
ライブデモ
HTML
html
<div class="controls">
<button id="script-error" type="button">Generate script error</button>
<img class="bad-img" />
</div>
<div class="event-log">
<label>Event log:</label>
<textarea readonly class="event-log-contents" rows="8" cols="30"></textarea>
</div>
JS
js
const log = document.querySelector('.event-log-contents');
window.addEventListener('error', (event) => {
log.textContent = log.textContent + `${event.type}: ${event.message}\n`;
console.log(event)
});
const scriptError = document.querySelector('#script-error');
scriptError.addEventListener('click', () => {
const badCode = 'const s;';
eval(badCode);
});
結果
仕様書
| Specification |
|---|
| HTML Standard # event-error |
| HTML Standard # handler-onerror |
ブラウザーの互換性
BCD tables only load in the browser
関連情報
Windowを対象としたこのイベント:errorイベント