Event handling in Javascript
September 21st, 2004I have been learning as much as I can about the onkeydown event handler and the event model in Javascript. I found that in Safari on MacOS X that each time I pressed a key it would make a noise. I wanted to stop it. By digging around the Mozilla DOM Reference I found the stopPropogation method. Before I thought to indicate an event was handled or not the handler function would return true or false. Apparently event.stopPropogation() is the proper method to call to achieve this goal. Unfortunately this did not work perfectly for Mozilla, MSIE and Safari. I had to work around it...
For starters, MSIE seems to insist that I reference the event as window.event and it also does not like it when I call event.stopPropogation(). So a little code workaround is necessary.
if (window.event && window.event.cancelBubble !== null) {
window.event.cancelBubble = true;
}
else {
event.stopPropagation();
}
Here I tell the event to cancel the bubble behavior in MSIE while Safari and Mozilla will tell the event to stop propogation. Without further documentation from Microsoft I can only guess this is how it should be done. All I really know is that it works. It seems the cowboy days of Javascript are still in effect, but at least the workaround code is much more elegant than the the Netscape 4 and MSIE 4 days.
To make things a little easier I am hoping the Javascript Editor for Eclipse improves to that point that it delivers on it's feature list. I have yet to see it in action as it does not seem to have a clean installer or documentation explaining how to use it. It seems it does not have much working yet. All it seems to do for me is provide a script icon for files with a Javascript extension.
