Perspectives

…On Ideas, Startups, Technology, Internet, India and Myself.

Firing a native event with Javascript..

without comments

Tech post after a really long time. And probably my first real piece of advicing in Javascript. So, take it with a pinch of salt. And tell me if I am writing something incorrect.

Ever wondered if it was possible to simulate a native event from your javascript code? Yes it is! Generally, you would probably have registered for an event and written all the logic in that callback. But sometimes, a need to do execute the same logic arises due to a completely unrelated event. To give you a real example, head over to this view of Yahoo! India maps. If you notice, clicking on any of the “local search element” on the right pane shades the element and also opens the marker. Now, clicking marker does the same thing too.

Instead of writing the code to shade the element all over again on the marker’s click callback, what you could do is to fire a click element on the corresponding search element on the right pane.

As you would expect you would have to do this differently in firefox and IE. And it will look something like this:

incallbackfunction() {
var fireOnThis = document.getElementById(“yourunrelatedelement”);
If(IE) {
var e = document.createEventObject();
fireOnThis.fireEvent(“onclick”,e);
}
else {
var e = document.createEvent(“MouseEvents”);
e.initMouseEvent(‘click’,true,true,window,0,0,0,0,0,false,false,false,false,0,null);
fireOnThis.dispatchEvent(e);
}
}

There are too many things to explain, but you should be able to get a hold of what is what with a little bit of search.

Related posts:

  1. Write in LJ and read in Blogspot
  2. Write in LJ and read in Blogspot
  3. Creating your own firefox search plugin
  4. Screw bars..aka..Tool bars
  5. Usability: Auto Saving users’ work

Written by shivku

April 13th, 2007 at 5:59 am

Posted in Uncategorized

Tagged with , , , , , ,

Leave a Reply