Tuesday, December 20, 2005

Javascript document.onclick

document.onclick=WindowClickEventExplorer;
function WindowClickEventExplorer(e){
if (navigator.appName=="Netscape") {
popIt=0;
}
if (navigator.appName.indexOf("Microsoft")!=-1) {
var WinEvent = window.event.srcElement.tagName;
if (WinEvent =='INPUT' || WinEvent =='IMG' || WinEvent =='A' || WinEvent =='B' || WinEvent =='SELECT') popIt=0;
}
}


The above code captures all the click events within the document inside the current browser and calls function WindowClickEventExplorer at the mouse click. Inside function WindowClickEventExplorer, appName property of javascript navigator object is used to identify the user's browser, which in this case is either Netscape or IE. If the browser is IE, we can use the following line of code window.event.srcElement.tageName to identify the tag name of the source that generates the click event.

No comments: