Skip to content

Instantly share code, notes, and snippets.

@lean8086
Created July 20, 2012 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lean8086/3147862 to your computer and use it in GitHub Desktop.
Save lean8086/3147862 to your computer and use it in GitHub Desktop.
Creating, triggering and listening a custom event
// Defining the custom event
var customEvent = (function () {
"use strict";
// Create a generic event
var e = document.createEvent("Event");
// Define the custom event name
e.initEvent("custom", true, true);
// Grab event to trigger
return e;
}());
// Trigger the event
document.dispatchEvent(customEvent);
// Listen for the event
document.addEventListener("custom", function () { ... });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment