Firefox: pass event from anonymous javascript function

This shorthand code runs an inline event - the "event" is passed to the testKeyPress function

<textarea id="source"
    onkeydown= "showCursPos(this); 
    var tf=testKeyPress(event);
    document.onkeypress=function(){return tf};
    document.onkeydown=function(){return tf}; " ></textarea>

function testKeyPress(e){
    if (e.ctrlKey==true ){
        if (e.which == null ){kcode=e.keyCode; }  // IE
        else if (e.which > 0){kcode=e.which; }    // gecko
        return testValidity(kcode);   //returns true-false
    }
}

      

However, in this anonymous version, the event is not passed to gecko:

<textarea id="source"></textarea>

$("source").onkeydown = function(){ 
    showCursPos(this);  // this function works
    // next event is passed in IE, but not gecko
    var tf=testKeyPress(event); 
    // remaining functions work if value is forced
    document.onkeypress=function(){return tf}; 
    document.onkeydown=function(){return tf}; 
    }

      

How do I pass a custom event to a function?

0


source to share


2 answers


Yes, there is an event object as arguments.

You can get it

var e=arguments[0] || event; // Firefox via the argument, but IE don't

      



I don't know if they are exactly the same, but I read <xxx onkeydown="func(event);">

likexxx.ononkeydown=function(event){func(event);};

Link event @ Mozilla.org

+3


source


Worked like a charm. This modification of my source code successfully passes the event from anonymous function to named function in my four browsers: IE6, FF2, NS7.2, OP9.22



$("source").onkeydown = function(){ 
    var e=arguments[0] || event; 
    showCursPos(this);  
    var tf=testKeyPress(e); 
    document.onkeypress=function(){return tf}; 
    document.onkeydown=function(){return tf}; 
}

      

0


source







All Articles