Thursday, June 12, 2008

Event in javascript

1. Event Object

In javascript event also an object that can be attached to any element. In IE event object is a global that can be access anywhere in the page (e = window.event), whereby other browser beside IE, the event object (e) is need to passed in with call back function
eg. element.onclick = function( /*For none IE browser*/e){
if(e) {
alert("this is none IE event object" + e)
}else{
e = window.event;
alert("this is IE event object" + e)
}
}

2. Stop Event Capturing and Bubbling

IE : window.event.cancelBubble = true
Mozilla: e.stopPropogation()

3. Stop Default Browser event

A browser event is an event that fired by the browser which you don't really realize about it, like when you right click on any element, the browser will fire its event by default. To stop that let look at this.
IE : window.event.returnValue = false;
Mozilla : e.preventDefault();

4. Attach Event to An Element

There are two ways which you can attach event to any dom element.
1. Using classical way
element.onclick = function(){alert("event click happens")};
2. Using attachEvent/addEventListener
Mozilla : dom.addListener("click",function(){}, false/true );

No comments: