Skip to content

Instantly share code, notes, and snippets.

@icecreamliker
Created July 12, 2015 10:07
Show Gist options
  • Select an option

  • Save icecreamliker/88f2f14f8b1920676fe2 to your computer and use it in GitHub Desktop.

Select an option

Save icecreamliker/88f2f14f8b1920676fe2 to your computer and use it in GitHub Desktop.
create custom event
var event = new Event('yaoli');
// Listen for the event.
elem.addEventListener('yaoli', function (e) { console.debug(e); }, false);
// Dispatch the event.
elem.dispatchEvent(event);
// =====================================
// Custom Event
var event = new CustomEvent('yaoli', { 'detail': 'yaoli is a good guy!' });
elem.addEventListener('yaoli', function (e) { console.debug(e.detail); }, false);
elem.dispatchEvent(event);
// =====================================
// Deprecated way
// Create the event.
var event = document.createEvent('Event');
// Define that the event name is 'build'.
event.initEvent('yaoli', true, true);
// Listen for the event.
document.addEventListener('yaoli', function (e) {
// e.target matches document from above
}, false);
// target can be any Element or other EventTarget.
document.dispatchEvent(event);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment