Events are the heart of any JavaScript application. As we see in most of the cases the scripts are triggered by Events.But the cross browser support is the main thing which we should keep in mind while handling events in JavaScript.
Lets look at how the event Object is supported in Internet Explorer(IE) and FireFox(FF) and Chrome etc.- event is referred as event Object in Firefox and chrome.
- window.event is referred as event Object in Internet Explorer
if (!event) event=window.event;
Also the one more advantage of using event Object is that we can get the target element(element on which this event is occurring).But different browser uses the different way of catching the target Element.So how we can get target in different browser can be as given below.
if (! event.target ) target =event.srcElement ;//IE
else target=event.target ;//Firefox
if (target.nodeType==3) target=target.parentNode ;//safari
Also the one more advantage of using event Object is that we can get the target element(element on which this event is occurring).But different browser uses the different way of catching the target Element.So how we can get target in different browser can be as given below.
- event.target gets the target element in Firefox,chrome.
- event.srcElement gets the target element in Internet Explorer.
- also for target node Type is equal to 3 then that browser is safari and we have to do target. parent Node to get target element.
if (! event.target ) target =event.srcElement ;//IE
else target=event.target ;//Firefox
if (target.nodeType==3) target=target.parentNode ;//safari