--- title: CustomEvent slug: Web/API/CustomEvent tags: - 待翻譯 translation_of: Web/API/CustomEvent ---
{{APIRef("DOM")}}
CustomEvent
interface 是應用程式為了任意目的所初始化的事件。
CustomEvent。
此介面繼承了其父介面 {{domxref("Event")}} 的屬性:
{{Page("/zh-TW/docs/Web/API/Event", "屬性")}}
初始化一 CustomEvent
object。若該事件已經被觸發,則不會進行任何動作。
此介面繼承了其父介面 {{domxref("Event")}} 的方法:
{{Page("/zh-TW/docs/Web/API/Event", "方法")}}
Specification | Status | Comment |
---|---|---|
{{SpecName('DOM WHATWG','#interface-customevent','CustomEvent')}} | {{Spec2('DOM WHATWG')}} | 原始定義 |
{{Compat("api.CustomEvent")}}
當要從 privileged code (像是插件)到非 privileged code (例如網頁)執行 CustomEvent ,你必須要考慮這之間的安全性。Firefox 和其他 Gecko 應用會對此有所限制。雖然這可以自動防止安全漏洞發生,但也可能導致您的程式碼沒辦法正常執行。
When creating a CustomEvent object, you must create the object from the same window as you're going to fire against. The detail
attribute of your CustomEvent will be subject to the same restrictions. String and Array values will be readable by the content without restrictions, but custom Objects will not. If 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); }
Note that exposing a function will allow the content script to run it with chrome privileges, which can open a security vulnerability.