|
|
|
|
|
|
| |
What I have provided is a cross browser approach to attach and manage events. IE uses Object.attachEvent to attach an event to an object, whereas Fx uses Object.addEventListener to do the above function. Similarly the functions used for removing an attached event also varies. What I have provided is a crossbrowser function addEvent which will call the function specific for that browser. Similarly calling the function preventDefault from an attached event handle prevents the triggering of the default action associated with that event.
My example uses the addEvent function to attach the function init to the onload event of the document. Which in turn attaches the function clickTest to the onclick event of the div ElemId. Now when you click on the div, the function clickTest is called which alerts that the element has been clicked on. Once the element is clicked upon, the function clickTest attached with the div is removed so that clicking on the div again will not show the alertbox.
In simple words,
1. init function attached with document load
2. init function attaches clickTest to onClick event of div so that a alert box shows when it is clicked.
3. clickTest detaches itself from the onClick event of div So clicking on the div will show an alertbox the first time.
addEvent - Use this function to add a cross browser event listener to an element
removeEvent - Use this function to remove an added event listener
preventDefault - Use this function to prevent the default action associated with an event
stopPropagation - Use this function to prevent the bubbling of the event.
| <!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>Cross Browser Event Management</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
<!--
/*
Cross Browser Event Management
*/
function addEvent(el, evType, handle){
if(el.addEventListener)el.addEventListener(evType, handle, false);
else if(el.attachEvent){
el["e" + evType + handle] = handle;
el[evType + handle] = function(){el["e" + evType + handle](window.event)}
el.attachEvent("on" + evType, el[evType + handle]);
}
}
function removeEvent(el,evType,handle){
if(el.removeEventListener)el.removeEventListener(evType, handle, false);
else if(el.detachEvent){
el.detachEvent("on" + evType, el[evType + handle])
el[evType + handle] = null
el["e" + evType + handle] = null;
}
}
function preventDefault(evt){
var e = evt ? evt : window.event;
if(e.preventDefault){
e.preventDefault();
}else{
e.returnValue = false;
}
}
function stopPropagation(evt){
var e = evt ? evt : window.event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
}
function init(){
addEvent(document.getElementById('ElemId'), 'click', clickTest);
}
function clickTest(){
alert('Clicked');
removeEvent(document.getElementById('ElemId'), 'click', clickTest);
alert('Removed');
}
addEvent(window, 'load', init);
//-->
</script>
</head>
<body onload="">
<div id="ElemId" style="width:200px;height:50px;border:1px #CCC solid;background:#F2F2F2;">Click Me</div>
</body>
</html> | | |
|
| Get your browser details using javascript Categories : Java Script, Beginner Guides, Browsers | | | Bouncing Marquee at Status Bar Categories : Java Script, HTML, Browsers | | | Flash Your Message at Status Bar Categories : Java Script, Browsers | | | Zephyr: AJAX Based Framework for PHP5 Developers Categories : PHP, AJAX, Frameworks, Java Script, Web Applications | | | javascript doesn't accept a fieldname with [] Categories : Java Script | | | Array values from javascript to php Categories : PHP, Java Script, Arrays | | | Create HTML forms dynamicly using Javascript & PHP Categories : PHP, PHP Classes, Java Script | | | Linked comboboxes with php-mysql & javascript Categories : PHP, Java Script, Databases, MySQL | | | The toll booth Categories : PHP, Java Script, Filesystem | | | MD5 secured login Categories : PHP, Java Script, Authentication, Security | | | Flash Detection Script with loads of features Categories : Java Script, Flash | | | Bypassing Plugin / FLASH Activation Categories : Java Script, Web Browsers, Security, Flash | | | write an event to the eventlog via JScript Categories : Java Script, Windows 2000 | | | complete sql to html table class.features filtering, forming variable
declaration,editing,hidden params.inspired from class html_table in
example table.php but bad thing it is PHP4 beta 2!! and sybase only Categories : Databases, HTML and PHP, Arrays, Browsers, Sybase | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | |
|
|
|