JSL.event

From Projects Wiki

Jump to: navigation, search

All the event related functions are in this class.

JSL.event Code

Contents

[edit] Methods

[edit] JSL.event.add( ele, type, func, capture )

The famous addEvent function. But calling it in this format is not the preffered method. Use the DOM interface to make the call.

[edit] Arguments

ele
The Element to which the event should be attached. This must be a DOM Node.
type
The event type - "load", 'mouseover', 'click', etc.
func
The function that must be called on the event
capture
true if you want to enable capture.

[edit] Example

JSL.dom("#ele").on("mouseover", function(e){ alert("Hello World"); });
JSL.event().add(document.getElementById("ele"), "mouseover", function(e){ alert("Hello World"); });

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_event.js, Line 22
function(ele,type,func,capture) {
    function _makeCallback(e){
        var ele = JSL.event(e).getTarget() || document;
        func.call(ele,e);
    }
    capture = capture||true;
 
    if(ele.attachEvent) {
        return ele.attachEvent('on' + type, _makeCallback);
    } else if(ele.addEventListener) {
        ele.addEventListener(type, _makeCallback, capture);
        return true;
    } else {
        ele['on' + type] = _makeCallback;
    }
}

[edit] JSL.event.stop( )

Stop an event from further propogation. Taken from http://www.openjs.com/articles/prevent_default_action/

[edit] Example

JSL.event(e).stop();

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_event.js, Line 45
function(){
    var e = this.event;
    e.cancelBubble = true;
    e.returnValue = false;
    if(e.stopPropagation) e.stopPropagation();
    if(e.preventDefault) e.preventDefault();
    return false;
}

[edit] JSL.event.getTarget( )

Get the target of the current event

[edit] Example

var ele = JSL.event(e).getTarget();

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_event.js, Line 59
function() {
    var element;
    var e = this.event;
    if(e.target) element=e.target;
    else if(e.srcElement) element=e.srcElement;
 
    if(element && element.nodeType==3) element=element.parentNode; //Safari Bug fix
    return element;
}
Personal tools