Return to Snippet

Revision: 34716
at October 27, 2010 17:25 by madr


Updated Code
/*! 
light-weight click listener, v 1.0
Copyright (c) 2010 by madr <http://madr.se>
Licensed under the MIT license: http://en.wikipedia.org/wiki/MIT_License
*/
/*
Usage:
Alter the below snippet for your needs and put the modified snippet in a js-file or a <script> and add it to your document. If your webapp depends on libraries or other resources to load, you better keep that in mind.
*/
(function(document, undefined) {
    function investigate(elm){
        /*
        Change the content of this function
        to suit your web application.
        */

        /*
        EXAMPLE 0: do nothing until jQuery (or other libraries) is loaded.
        if (typeof window.jQuery == 'undefined') { return false; }
        */
        
        /*
        EXAMPLE 1: look for element type
        if (elm.nodeName == 'A') {
            // do stuff ...
            return true;
        }
        */
        
        /*
        EXAMPLE 2: look for id or other property
        if (elm.id == 'modal-window-opener') {
            // do stuff ...
            return true;
        }
        */
        
        /*
        EXAMPLE 3: sniffing a classname
        if (elm.className.match(/read-more/)) {
            // do stuff ...
            return true;
        }
        */    
        
        return false; // default
    }
    
    function clicklistener(evt){
        var event = evt || window.event,
            elm = event.target || window.srcElement,
            magic_did_happen = investigate(elm);
        
        if (magic_did_happen) {
            if (window.event) { window.eventReturnValue = false; } 
            else { evt.preventDefault(); }

            return false;
        }
    }
    
    if (document.attachEvent) { 
        document.attachEvent('onclick', clicklistener); 
    } else {
        document.addEventListener('click', clicklistener, false); 
    }
})(document);

Revision: 34715
at October 27, 2010 17:13 by madr


Initial Code
// light-weight click listener, v 1.0
// Copyright (c) 2010 by madr <http://madr.se>
// Mit license style.
(function(document, undefined) {
    function investigate(elm){
        /*
        Change the content of this function
        to suit your web application.
        */
        
        /*
        EXAMPLE 1: look for element type
        if (elm.nodeName == 'A') {
            // do stuff ...
            return true;
        }
        */
        
        /*
        EXAMPLE 2: look for id or other property
        if (elm.id == 'modal-window-opener') {
            // do stuff ...
            return true;
        }
        */
        
        /*
        EXAMPLE 3: sniffing a classname
        if (elm.className.match(/read-more/)) {
            // do stuff ...
            return true;
        }
        */    
        
        return false; // default
    }
    
    function clicklistener(evt){
        var event = evt || window.event,
            elm = event.target || window.srcElement,
            magic_did_happen = investigate(elm);
        
        if (magic_did_happen) {
            if (window.event) { window.eventReturnValue = false; } 
            else { evt.preventDefault(); }

            return false;
        }
    }
    
    if (document.attachEvent) { 
        document.attachEvent('onclick', clicklistener); 
    } else {
        document.addEventListener('click', clicklistener, false); 
    }
})(document);

Initial URL

                                

Initial Description

                                

Initial Title
click listener and click event delegation function, library independent

Initial Tags
javascript

Initial Language
JavaScript