custom event in javascript and jquery

时间:2021-01-23 20:12:29

javascript:

// add a eventListener
document.addEventListener("abc", function(){alert('this is abc handler'}, false); //fire event
document.dispatchEvent(new CustomEvent("abc", {
detail: {
message: msg,
time: new Date(),
},
bubbles: true,
cancelable: true
}));

jQuery

$(document).on('abc',function(){alert('this is abc handler on document'});

$.event.trigger({
type: "abc",
message: "Hello World!",
time: new Date()
});

reference:

How to Create Custom Events in JavaScript

http://www.sitepoint.com/javascript-custom-events/

How to Create Custom Events in jQuery

http://www.sitepoint.com/jquery-custom-events/