SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)

    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...

    Code javascript:
    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.

  2. #2
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Found a solution. Distasteful but serves my purposes.

    Code javascript:
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •