CustomEvent

CustomEvent interface represents events initialized by an application for any purpose.

注意: 此特征可用于 Web 工作者 .

构造函数

CustomEvent()
创建 CustomEvent .

特性

CustomEvent.detail 只读
Any data passed when initializing the event.

This interface inherits properties from its parent, Event :

Event.bubbles 只读
A boolean indicating whether or not the event bubbles up through the DOM.
Event.cancelBubble
A historical alias to Event.stopPropagation() . Setting its value to true before returning from an event handler prevents propagation of the event.
Event.cancelable 只读
A boolean indicating whether the event is cancelable.
Event.composed 只读
A boolean indicating whether or not the event can bubble across the boundary between the shadow DOM and the regular DOM.
Event.currentTarget 只读
A reference to the currently registered target for the event. This is the object to which the event is currently slated to be sent. It's possible this has been changed along the way through retargeting .
Event.deepPath
An Array of DOM Node s through which the event has bubbled.
Event.defaultPrevented 只读
Indicates whether or not the call to event.preventDefault() canceled the event.
Event.eventPhase 只读
Indicates which phase of the event flow is being processed.
Event.explicitOriginalTarget 只读
The explicit original target of the event (Mozilla-specific.)
Event.originalTarget 只读
The original target of the event, before any retargetings. (Mozilla-specific.)
Event.returnValue
A historical property introduced by Internet Explorer and eventually adopted into the DOM specification in order to ensure existing sites continue to work. Ideally, you should try to use Event.preventDefault() and Event.defaultPrevented instead, but you can use returnValue if you choose to do so.
Event.srcElement
A non-standard alias (from old versions of Microsoft Internet Explorer) for Event.target . Some other browsers are starting to support it for web compatibility purposes.
Event.target 只读
A reference to the target to which the event was originally dispatched.
Event.timeStamp 只读
The time at which the event was created (in milliseconds). By specification, this value is time since epoch—but in reality, browsers' definitions vary. In addition, work is underway to change this to be a DOMHighResTimeStamp 代替。
Event.type 只读
The name of the event. Case-insensitive.
Event.isTrusted 只读
Indicates whether or not the event was initiated by the browser (after a user click, for instance) or by a script (using an event creation method, like Event.initEvent ).

过时特性

Event.scoped 只读
Obsolete; use composed instead. (A Boolean indicating whether the given event will bubble across through the shadow root into the standard DOM.)

方法

CustomEvent.initCustomEvent()

Initializes a CustomEvent object. If the event has already being dispatched, this method does nothing.

This interface inherits methods from its parent, Event :

Event.createEvent()

Creates a new event, which must then be initialized by calling its initEvent() 方法。

Event.composedPath()
Returns the event’s path (objects on which listeners will be invoked). This does not include nodes in shadow trees if the shadow root was created with its ShadowRoot.mode closed.
Event.initEvent()
Initializes the value of an Event created. If the event has already been dispatched, this method does nothing.
Event.preventDefault()
Cancels the event (if it is cancelable).
Event.stopImmediatePropagation()
For this particular event, prevent all other listeners from being called. This includes listeners attached to the same element as well as those attached to elements that will be traversed later (during the capture phase, for instance).
Event.stopPropagation()
Stops the propagation of events further along in the DOM.

Obsolete methods

Event.getPreventDefault()
Non-standard; use Event.defaultPrevented instead. (Returns the value of Event.defaultPrevented .)
Event.preventBubble() Obsolete since Gecko 24
Obsolete; use event.stopPropagation instead. (Prevents the event from bubbling.)
Event.preventCapture() Obsolete since Gecko 24
Obsolete; use event.stopPropagation 代替。

规范

规范 状态 Comment
DOM
The definition of 'CustomEvent' in that specification.
实时标准 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
CustomEvent Chrome 15 Edge 12 Firefox 6 IE 9 Opera 11 Safari 5.1 WebView Android ≤37 Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 6 Samsung Internet Android 1.0
CustomEvent() 构造函数 Chrome 15 Edge ≤18 Firefox 11 IE No Opera 11.6 Safari 6.1 WebView Android ≤37 Chrome Android 18 Firefox Android 14 Opera Android 12 Safari iOS 6.1 Samsung Internet Android 1.0
detail Chrome 15 Edge 14 Firefox 11 IE No Opera 11.6 Safari 6.1 WebView Android Yes Chrome Android Yes Firefox Android 14 Opera Android Yes Safari iOS 6.1 Samsung Internet Android Yes
initCustomEvent
弃用
Chrome Yes
Yes
59
canBubble , cancelable ,和 detail are optional parameters defaulting to false , false ,和 null 分别。
Edge 14 Firefox 6 IE 9 Opera 11 Safari 5.1 WebView Android Yes
Yes
59
canBubble , cancelable ,和 detail are optional parameters defaulting to false , false ,和 null 分别。
Chrome Android Yes
Yes
59
canBubble , cancelable ,和 detail are optional parameters defaulting to false , false ,和 null 分别。
Firefox Android 6 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
Yes
7.0
canBubble , cancelable ,和 detail are optional parameters defaulting to false , false ,和 null 分别。
Available in workers Chrome Yes Edge 12 Firefox 48 IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 48 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持
完整支持
不支持
不支持
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。

Firing from privileged code to non-privileged code

When firing a CustomEvent from privileged code (i.e. an extension) to non-privileged code (i.e. a webpage), security issues should be considered. Firefox and other Gecko applications restrict an object created in one context from being directly used for another, which will automatically prevent security holes, but these restrictions may also prevent your code from running as expected.

While creating a CustomEvent object, you must create the object from the same window detail attribute of your CustomEvent will be subjected to the same restrictions. String and Array values will be readable by the content without restrictions, but custom Object will not. While using a custom object, you will need to define the attributes of that object that are readable from the content script using Components.utils.cloneInto() .

// doc is a reference to the content document
function dispatchCustomEvent(doc) {
  var eventDetail = Components.utils.cloneInto({foo: 'bar'}, doc.defaultView);
  var myEvent = doc.defaultView.CustomEvent("mytype", eventDetail);
  doc.dispatchEvent(myEvent);
}
					

But one needs to keep in mind that exposing a function will allow the content script to run it with chrome privileges, which can open a security vulnerability.

另请参阅