WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Cross browser event management functions using JavaScript.
Categories : Java Script, Browsers Click here to Update Your Picture
Bobin R
Date : Jan 30th 2007
Grade : 5 of 5 (graded 1 times)
Viewed : 5963
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Bobin R
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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>



Bouncing Marquee at Status Bar
Categories : Java Script, HTML, Browsers
Flash Your Message at Status Bar
Categories : Java Script, Browsers
Get your browser details using javascript
Categories : Java Script, Beginner Guides, Browsers
How to force the user to download a file instead of opening it up in an controlled environment within the browser (i.e. MS Word/Adobe Acrobat)
Categories : Browsers, PHP, HTTP
Hover 98 - a simple PHP-script to generate OnMouseOver-JavaScript-Code the easy way.
Categories : Complete Programs, Java Script
How to make IE open more than the 2 default connections to a server?
Categories : Browsers, General
PHP Array to Javascript Object
Categories : PHP, Arrays, Java Script
JavaScript Fade In Text Messages
Categories : Java Script, DHTML
Bookmarklets are simple tools that extend the surf and search capabilities of Netscape and Explorer web browsers.
Categories : Java Script, HTML, General
Remote Scripting: send form POST data to a script and insert the results into a page without refreshing the page.
Categories : PHP, AJAX, HTML and PHP, Java Script
JavaScript dropdown list menu to switch any page.
Categories : Java Script, Beginner Guides, Form Processing
Linked comboboxes with php-mysql & javascript
Categories : PHP, Java Script, Databases, MySQL
Local Time clock and Server time usign PHP and JavaScript
Categories : PHP, Java Script, Date Time, Beginner Guides
Tree Menu Dynamic (+Static) with Loading in Progress..
Categories : PHP, Java Script, HTML and PHP
Disabling Image Toolbar in Internet Explorer 6+
Categories : Browsers, Meta Tags, Tip