91

I need to pass data between two autonomic user scripts - ideally without touching the unsafeWindow object - and I thought using custom events would be the way to go. I thought of something like this (let us disregard the MSIE model for the purpose of the example):

addEventListener("customEvent", function(e) {
  alert(e.data);
});

var custom = document.createEvent("HTMLEvents");
custom.initEvent("customEvent", true, true);
custom.data = "Some data...";
dispatchEvent(custom);

This works nicely in the standard Javascript environment and within one user script, but when the event is fired by the user script and caught outside of it or inside another user script, the data property is undefined in Chromium. I suppose I could just save the passed data in the sessionStorage, but it is far from seamless. Any other elegant solutions? Perfection need and can be achieved, I can feel it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Witiko
  • 3,167
  • 3
  • 25
  • 43

3 Answers3

147

Yes, you can use a MessageEvent or a CustomEvent.

Example usage:

//Listen for the event
window.addEventListener("MyEventType", function(evt) {
    alert(evt.detail);
}, false);

//Dispatch an event
var evt = new CustomEvent("MyEventType", {detail: "Any Object Here"});
window.dispatchEvent(evt);
Digital Plane
  • 37,354
  • 7
  • 57
  • 59
  • 34
    To achieve this using the `new CustomEvent('eventName')` constructor, pass the data in a CustomEventInit hash, keyed by 'detail' like this: `new CustomEvent('eventName', {'detail': data});`. – Capi Etheriel Nov 05 '12 at 18:53
  • 3
    I s that data serialized? Can I put a reference to an HTML element in there? – Vlas Bashynskyi Nov 02 '15 at 13:14
  • 8
    +1 for using `CustomEvent` as well. Note that you should use `detail` property, only this will be available under `event.detail` in the listener. – ivkremer Sep 01 '16 at 14:48
  • This is a really confusing answer. Is it new CustomEvent or document.createEvent ? – Phil Feb 20 '18 at 14:06
  • 1
    For me `detail` is in `event.originalEvent`. – Kaushal Jun 30 '18 at 12:04
  • Worth mentioning about detail property -> https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/detail – Amruth Feb 10 '23 at 07:48
17

pass object with more details as attributes:

var event = new CustomEvent('build', { detail: { 'detail1': "something", detail2: "something else" }});

function eventHandler(e) {
  log('detail1: ' + e.detail.detail1);
  log('detail2: ' + e.detail.detail2);
}

https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events

404
  • 8,022
  • 2
  • 27
  • 47
Amit G
  • 2,293
  • 3
  • 24
  • 44
  • 13
    This is not JSON: {foo: "bar"} It's a JavaScript hash. JSON has very strict rules (keys must be enclosed in double quotes), and even when the same JS would be valid JSON, using it inside JS is just JS and not JSON. In my opinion it's only JSON if it's valid JSON and it is stored as a string. When it's JS code it's just a JavaScript datastructure that happens to look like JSON. – joonas.fi Mar 12 '15 at 11:31
  • What are the exact differences between hashes and Json objects? As far as I know we can make a hash like var xyz={}; xyz['index1']="myvalue1"; xyz['index2']="myvalue2"; – Sujal Mandal Dec 02 '15 at 07:13
  • 1
    @SujalMandal - the difference is that he's not using the term JSON correctly at all. That's not JSON, that's just a javascript object literal. JSON is a **string** format which is is used to transfer data between programs/store data and its syntax is heavily based on javascript objects. – Jimbo Jonny Mar 08 '16 at 04:00
  • 3
    Need to wrap `'detail1': "something", detail2: "something else"` in a property named `detail` in order for it to work: `var event = new CustomEvent('build', { detail: { 'detail1': "something", detail2: "something else" } });` – Jay Apr 25 '18 at 15:52
8

new CustomEvent is not supported in IE https://caniuse.com/#search=CustomEvent

Here is a version which also works on IE9+:

//Listen for the event
window.addEventListener("MyEventType", function(evt) {
     alert(evt.detail.test); //alerts "Any Object Here"
}, false);

 //Dispatch an event
 var evt = document.createEvent('CustomEvent');
 evt.initCustomEvent('MyEventType', false, false, { test: "Any Object Here" });
 window.dispatchEvent(evt);
George Chondrompilas
  • 3,167
  • 27
  • 35
  • 4
    This solution has been deprecated. A more futureproof way to support IE9+ is to use a polyfill. See also https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/initCustomEvent – jnaklaas Aug 10 '21 at 08:49
  • 2023 - anyone still using IE9 is probably used to 99% of the internet not working for them anymore. :) – maxshuty May 03 '23 at 00:14