I am tasked to fire custom events on the document without using any libraries like jQuery or prototype.
So I'm ok on Firefox doing this:
function fireCustomEvent(eventData)
{
if (document.createEvent) // Firefox
{
var event = document.createEvent('HTMLEvents'); // create event
event.initEvent('myCustomEvent', true, true ); // name event
event.data = eventData; // put my stuff on it
document.dispatchEvent(event); // fire event
}
else if (document.createEventObject) // IE
{
xxxxxxxxxxx
}
}
and now I can fire it like this:
fireCustomEvent({
category: 'test',
value: 123
});
and catch it like this (here I can use jQuery):
$(document).bind('myCustomEvent', function (event) {
doStuff(event);
});
My question is, what can I do to make this work on IE (in other words, where I put the xxxxxxxxxxx )?
I think that the IE-equivalent should look something like this:
var event = document.createEventObject();
event.data = eventData;
document.fireEvent('myCustomEvent', event);
But that doesn't work. IE lets me use only predefined event-names (onclick, etc) and even some of those don't work (onmessage for example)
Any help or ideas are appreciated!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…