Skip to content

Instantly share code, notes, and snippets.

@albburtsev
Created April 4, 2012 10:31
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 albburtsev/2300211 to your computer and use it in GitHub Desktop.
Save albburtsev/2300211 to your computer and use it in GitHub Desktop.
function fireCustomEvent(eventType) {
if ( document.dispatchEvent ) { // DOM browser
var e = document.createEvent("UIEvents");
e.initEvent(eventType, false, false);
document.dispatchEvent(e);
} else if ( document.attachEvent ) { // IE
if ( !document.documentElement[eventType] )
document.documentElement[eventType] = 1;
else
document.documentElement[eventType] += 1;
}
};
$.listenCustomEvent = function(eventType, callback) {
$(document).on(eventType, callback);
$(document.documentElement).on('propertychange', function(e) {
if (e.originalEvent.propertyName == eventType)
callback();
});
};
// Use case
$.listenCustomEvent('wa-test', function() {
alert('It\'s worked!');
});
fireCustomEvent('wa-test');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment