The preventDefault() method from zEvent is used to prevent the running flag from being set to true : zEvents « Event « JavaScript Tutorial

JavaScript Tutorial
1. Language Basics
2. Operators
3. Statement
4. Development
5. Number Data Type
6. String
7. Function
8. Global
9. Math
10. Form
11. Array
12. Date
13. Dialogs
14. Document
15. Event
16. Location
17. Navigator
18. Screen
19. Window
20. History
21. HTML Tags
22. Style
23. DOM Node
24. Drag Drop
25. Object Oriented
26. Regular Expressions
27. XML
28. GUI Components
29. Dojo toolkit
30. jQuery
31. Animation
32. MS JScript
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript Tutorial » Event » zEvents 
15. 11. 3. The preventDefault() method from zEvent is used to prevent the running flag from being set to true
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
    <head>
        <title>Page title</title>
        <script type="text/javascript">
/*------------------------------------------------------------------------------
 * JavaScript zEvents Library
 * Version 1.1
 * by Nicholas C. Zakas, http://www.nczonline.net/
 * Copyright (c) 2004-2005 Nicholas C. Zakas. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *------------------------------------------------------------------------------
 */  

/**
 * Encapsulates information about an event.
 * @scope public
 * @class
 */
function zEvent() {

    /**
     * The type of event.
     * @scope public
     */
    this.type /*: String */   null;
    
    /**
     * The object that caused the event.
     * @scope public
     */
    this.target /*: zEventTarget */ null;
    
    /**
     * A secondary object related to the event.
     * @scope public
     */
    this.relatedTarget /*: zEventTarget */ null;
    
    /**
     * Indicates whether or not the event can be canceled.
     * @scope public
     */
    this.cancelable /*: boolean */ false;
    
    /**
     * The time that the event occurred.
     * @scope public
     */
    this.timeStamp /*: long */ null;
    
    /*
     * Set to false to cancel event.
     * @scope public
     */
    this.returnValue /*: boolean */ true;    
}

/**
 * Initializes the event object with information for the event.
 * @scope public
 @param sType The type of event encapsulated by the object.
 @param bCancelable True if the event can be cancelled.
 */
zEvent.prototype.initEvent = function (sType /*: String */,
                                       bCancelable /*: boolean */) {
    this.type = sType;
    this.cancelable = bCancelable;
    this.timeStamp = (new Date()).getTime();
};

/**
 * Prevents the default behavior for an event.
 * @scope public
 */
zEvent.prototype.preventDefault = function () {
    if (this.cancelable) {
        this.returnValue = false;
    }
};

/**
 * Any class that wants to support events should inherit from this.
 * @class
 * @scope public
 */
function zEventTarget() {

    /**
     * Array of event handlers.
     * @scope private
     */
    this.eventhandlers /*: Object */ new Object();
}

/**
 * Adds an event listener function to handle the type of event.
 * @scope public
 @param sType The type of event to handle (i.e., "mousemove", not "onmousemove").
 @param fnListener The listener function for the event.
 */
zEventTarget.prototype.addEventListener = function (sType /*: String */,
                                                    fnListener /*: Function */) {
    if (typeof this.eventhandlers[sType== "undefined") {
        this.eventhandlers[sTypenew Array;
    }   
    
    this.eventhandlers[sType][this.eventhandlers[sType].length= fnListener;                                                    
};

/**
 * Causes an event to fire.
 * @scope public
 @param oEvent The event object containing information about the event to fire.
 @return True if the event should continue, false if not.
 */
zEventTarget.prototype.dispatchEvent = function (oEvent /*: zEvent *//*: boolean */ {

    /*
     * Set the target of the event.
     */
    oEvent.target = this;

    /*
     * Call each event handler and pass in the event object.
     */
    if (typeof this.eventhandlers[oEvent.type!= "undefined") {
        for (var i=0; i < this.eventhandlers[oEvent.type].length; i++) {    
            this.eventhandlers[oEvent.type][i](oEvent);
        }
    }

    /*
     * Return the value of returnValue, which is changed to false
     * when preventDefault() is called.
     */
    return oEvent.returnValue;                                                   
};

/**
 * Removes an event listener function from handling the type of event.
 * @scope public
 @param sType The type of event to remove from (i.e., "mousemove", not "onmousemove").
 @param fnListener The listener function to remove.
 */
zEventTarget.prototype.removeEventListener = function (sType /*: String */,
                                                       fnListener /*: Function */) {
    if (typeof this.eventhandlers[sType!= "undefined") {
        var arrTemp = new Array;
        for (var i=0; i < this.eventhandlers[sType].length; i++) {
            if (this.eventhandlers[sType][i!= fnListener) {
                arrTemp[arrTemp.lengththis.eventhandlers[sType][i];
            }
        }
        this.eventhandlers[sType= arrTemp;
    }   
                                                   
};
        
        </script>
        <script type="text/javascript">
//sample class
function Car() {

    //inherit properties from zEventTarget
    zEventTarget.apply(this);

    //property indicating if the car is running
    this.running = false;
}

//inherit methods from zEventTarget
Car.prototype = new zEventTarget();

//method to start engine
Car.prototype.start = function () {

    //create event object
    var oEvent = new zEvent();

    //the event is called "start" and is cancelable
    oEvent.initEvent("start"true);

    //fire the event
    if (this.dispatchEvent(oEvent)) {

       //if dispatchEvent() returns true, set the flag
       this.running = true;
    }

};

//method to stop engine
Car.prototype.stop = function () {

    //create event object
    var oEvent = new zEvent();

    //the event is called "stop" and is cancelable
    oEvent.initEvent("stop"true);

    //fire the event
    if (this.dispatchEvent(oEvent)) {

        //if dispatchEvent() returns true, set the flag
        this.running = false;
    }

};             
        </script>
        <script type="text/javascript">
           //sample object
           var oCar = new Car();
           
           //assign event handler for "start" event
           oCar.addEventListener("start"function(oEvent) {
               alert("Cancelling the start...");
               oEvent.preventDefault();
           });
           
        </script>
 
 
    </head>
    <body>
        
        <script type="text/javascript">
        
            //call the start() method
            oCar.start();
            
            //now, what is the value of running?
            alert("Running: " + oCar.running);
        
        </script>
        <P>This example uses more complex event handlers and the <code>zEvent</code>
        object. Also, the <code>preventDefault()</code> method is used to prevent the <code>running</code>
        flag from being set to true.</p>
    </body>
</html>
15. 11. zEvents
15. 11. 1. This example uses simple event handlers to alert you when the start() and stop() methods are called
15. 11. 2. This example uses more complex event handlers and the zEvent object
15. 11. 3. The preventDefault() method from zEvent is used to prevent the running flag from being set to true
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.