Fire a change event. (Prototype Library in Use)

I’m working on a unit test for a javascript library that has several handlers. I wish to fire those handlers in a “natural” way, by raising an event that gets caught. I am having trouble creating an event that gets caught in Firefox.

I do development in Firefox to take advantage of Firebug, but the software is for a closed intranet and IE 7 is the required browser (we support IE 8 insofar as we force it to IE 8 mode). This policy predate my hire, I’m trying to get the software to a browser agnostic state - first I have to replace an ActiveX object known as MeadCo Script X - but that’s a whole other issue.

Anyway I need to raise events, specifically onchange events in Firefox. I have this so far…


function fire(el, ev) {
			el = $(el);

			if (typeof(el.fireEvent) != 'undefined') { // IE 
				el.fireEvent('on'+ev);
			} else { // Firefox?
				var e = document.createEvent('HTMLEvents');
				e.initEvent(el, true, true);
				el.dispatchEvent(e);
			}
		}

Prototype library is in use for this project, though I haven’t used it for this particular corner case.

Found a solution. Distasteful but serves my purposes.


function testEvent( e, ev, target) {
	e = $(e);
	var evt = Event.fire(e, ev);
	target( evt );
}

What’s distasteful is that the event routing isn’t working - the handler does not “naturally” catch the event. However, Event.fire returns the event and I can force feed the event to the target I’m testing.