Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOM Reference] Update CustomEvent interface reference #9920

Merged
merged 13 commits into from
Nov 22, 2021
49 changes: 9 additions & 40 deletions files/en-us/web/api/customevent/customevent/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,38 @@
title: CustomEvent()
slug: Web/API/CustomEvent/CustomEvent
tags:
- API
- Constructor
- CustomEvent
- Reference
- events
browser-compat: api.CustomEvent.CustomEvent
---
{{APIRef("DOM")}}

The **`CustomEvent()`** constructor creates a new
{{domxref("CustomEvent")}}.

{{AvailableInWorkers}}
The **`CustomEvent()`** constructor creates a new {{domxref("CustomEvent")}}.

## Syntax

```js
event = new CustomEvent(typeArg, customEventInit);
event = new CustomEvent(typeArg, customEventInit);
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved
```

### Parameters

- `typeArg`
- : A {{domxref("DOMString")}} representing the name of the event.
- : A string representing the name of the event.
- `customEventInit` {{optional_inline}}
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved

- : A `CustomEventInit` dictionary, having the following fields:
- : A dictionary, having the following fields:
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved

- `"detail"`, optional and defaulting to `null`, of type
any, that is an event-dependent value associated with the event.
- `"detail"`, optional and defaulting to `null`, of any type,
containing an event-dependent value associated with the event,
available to the handler using the {{domxref("CustomEvent.detail")}} property.
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved

> **Note:** _The `CustomEventInit`\*\* dictionary also accepts fields
> from the {{domxref("Event.Event", "EventInit")}} dictionary._
- Any field that can be used in the init object of the {{domxref("Event.Event", "Event()")}} constructor._

### Return value

A new `CustomEvent` object of the specified type, with any other properties
configured according to the `CustomEventInit` dictionary (if one was
provided).
configured according to the dictionary if one was provided.

## Example

Expand All @@ -67,30 +60,6 @@ Additional examples can be found at [Creating and triggering events](/en-US/docs

{{Compat}}

## Polyfill

You can polyfill the `CustomEvent()` constructor functionality in Internet
Explorer 9 and higher with the following code:

```js
(function () {

  if ( typeof window.CustomEvent === "function" ) return false;

function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: null };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}

window.CustomEvent = CustomEvent;
})();
```

Internet Explorer >= 9 adds a CustomEvent object to the window, but with correct
implementations, this is a function.

## See also

- {{domxref("CustomEvent")}}
Expand Down
20 changes: 4 additions & 16 deletions files/en-us/web/api/customevent/detail/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,17 @@
title: CustomEvent.detail
slug: Web/API/CustomEvent/detail
tags:
- API
- CustomEvent
- DOM
- Property
- Reference
- detail
- Read-only
browser-compat: api.CustomEvent.detail
---
{{APIRef("DOM")}}

The **`detail`** readonly property of the
{{domxref("CustomEvent")}} interface returns any data passed when initializing the
event.
The read-only **`detail`** property of the {{domxref("CustomEvent")}} interface
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved
returns any data passed when initializing the event.

{{AvailableInWorkers}}

## Syntax

```js
let myDetail = customEventInstance.detail;
```

### Return value
### Value
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved

Whatever data the event was initialized with.

Expand Down
30 changes: 4 additions & 26 deletions files/en-us/web/api/customevent/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
title: CustomEvent
slug: Web/API/CustomEvent
tags:
- API
- CustomEvent
- DOM
- Interface
- NeedsExample
- Reference
browser-compat: api.CustomEvent
---
Expand All @@ -19,18 +15,18 @@ The **`CustomEvent`** interface represents events initialized by an application
## Constructor

- {{domxref("CustomEvent.CustomEvent", "CustomEvent()")}}
- : Creates a `CustomEvent`.
- : Creates a new `CustomEvent`.

## Properties

_This interface inherits properties from its parent,_ {{domxref("Event")}}.
_This interface inherits properties from its parent, {{domxref("Event")}}._

- {{domxref("CustomEvent.detail")}} {{readonlyinline}}
- : Any data passed when initializing the event.
- : Returns any data passed when initializing the event.

## Methods

_This interface inherits methods from its parent,_ {{domxref("Event")}}.
_This interface inherits methods from its parent, {{domxref("Event")}}._

- {{domxref("CustomEvent.initCustomEvent()")}} {{deprecated_inline}}
- : Initializes a `CustomEvent` object. If the event has already being dispatched, this method does nothing.
Expand All @@ -43,25 +39,7 @@ _This interface inherits methods from its parent,_ {{domxref("Event")}}.

{{Compat}}

## 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](/en-US/docs/XUL/window). The `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()](/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.cloneInto).

```js
// 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.

## See also

- {{domxref("Window.postMessage()")}}
- [Interaction between privileged and non-privileged pages](/en-US/docs/Code_snippets/Interaction_between_privileged_and_non-privileged_pages)
- [Creating and triggering events](/en-US/docs/Web/Events/Creating_and_triggering_events)
26 changes: 10 additions & 16 deletions files/en-us/web/api/customevent/initcustomevent/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@
title: CustomEvent.initCustomEvent()
slug: Web/API/CustomEvent/initCustomEvent
tags:
- API
- CustomEvent
- DOM
- Deprecated
- Method
- Reference
browser-compat: api.CustomEvent.initCustomEvent
---
{{APIRef("DOM")}}{{Deprecated_header}}

The **`CustomEvent.initCustomEvent()`** method initializes a
`CustomEvent` object. If the event has already been dispatched, this method
does nothing.
The **`CustomEvent.initCustomEvent()`** method initializes a `CustomEvent` object.
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved
If the event has already been dispatched, this method does nothing.

Events initialized in this way must have been created with the {{
domxref("Document.createEvent()") }} method. This method must be called to set the event
before it is dispatched, using {{ domxref("EventTarget.dispatchEvent()") }}. Once
dispatched, it doesn't do anything anymore.
Events initialized in this way must have been created with the {{domxref("Document.createEvent()")}} method.
This method must be called to set the event before it is dispatched, using {{ domxref("EventTarget.dispatchEvent()") }}.
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved
Once dispatched, it doesn't do anything anymore.

> **Note:** **Do not use this method anymore, as it is deprecated.**
>
Expand All @@ -35,14 +30,14 @@ event.initCustomEvent(type, canBubble, cancelable, detail);
### Parameters

- `type`
- : Is a {{domxref("DOMString")}} containing the name of the event.
- _`canBubble`_
- : Is a string containing the name of the event.
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved
- `canBubble`
- : Is a boolean value indicating whether the event bubbles up through the DOM
or not.
- `cancelable`
- : Is a boolean value indicating whether the event is cancelable.
- _`detail`_
- : The data passed when initializing the event.
- `detail`
- : Any data, that will be available to the handler through the {{domxref("CustomEvent.detail")}} property.
teoli2003 marked this conversation as resolved.
Show resolved Hide resolved

## Specifications

Expand All @@ -55,5 +50,4 @@ event.initCustomEvent(type, canBubble, cancelable, detail);
## See also

- {{domxref("CustomEvent")}}
- The constructor to use instead of this deprecated method:
{{domxref("CustomEvent.CustomEvent", "CustomEvent()")}}.
- The constructor to use instead of this deprecated method: {{domxref("CustomEvent.CustomEvent", "CustomEvent()")}}.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is important enough that it should be at the top, not in what many may view as extra information.

(Has anyone discussed whether deprecated features should have a heading titled "Alternatives"?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already mentioned in the first section, at the top of the article, in the note:
Screenshot 2021-10-27 at 07 14 30

I think it does no harm to have it at both places, and I 100% agree it has to be mentioned at the top.

For an Alternatives, it is worth discussing. I don't know if we want a heading or a note, but we definitively need the information about the alternative (and near the top of the article) each time something is deprecated or non-standard, or people will continue to use it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does no harm to have it at both places

One such item does no harm. I do my best to minimize such repetitions. They are too easily missed during update passes.