Skip to content

Instantly share code, notes, and snippets.

@albburtsev
Created April 4, 2012 10:31
Show Gist options
  • 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