CustomEvent()
CustomEvent()
コンストラクターは新しい CustomEvent
を生成します。
注: この機能は Web Worker 内で利用可能です
構文
event = new CustomEvent(typeArg, customEventInit);
引数
返値
A new CustomEvent
object of the specified type, with any other properties configured according to the CustomEventInit
dictionary (if one was provided).
例
// add an appropriate event listener
obj.addEventListener("cat", function(e) { process(e.detail) });
// create and dispatch the event
var event = new CustomEvent("cat", {
detail: {
hazcheeseburger: true
}
});
obj.dispatchEvent(event);
その他の例は、イベントの作成とトリガ (en-US)にあります。
仕様書
仕様書 | 状態 | 備考 |
---|---|---|
DOM CustomEvent() の定義 |
現行の標準 | 初回定義 |
ブラウザーの対応
BCD tables only load in the browser
ポリフィル
Internet Explorer 9 以上では、 CustomEvent()
の機能を以下のコードで代替することができます。
(function () {
if ( typeof window.CustomEvent === "function" ) return false;
function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
CustomEvent.prototype = window.Event.prototype;
window.CustomEvent = CustomEvent;
})();
Internet Explorer 9 以上では CustomEvent オブジェクトを window に追加していますが、正しい実装では、これは関数です。