1 (edited by frameworker 2012-01-23 7:41:33 PM)

Topic: undefined variable "event"

Hi folks,

Creating JS events like this:

$objResponse->setEvent('myinputid','onkeypress',"alert(event.keyCode);");

produces and error in FF:

event is not defined
[Bei diesem Fehler anhalten] element=xajax.$(element);sEvent=xajax....ction() { '+code+'; }');return true;}

If the formular was created by an GET request like

<input type="text" name="myinputid" id="myinputid" onkeypress="alert(event.keyCode);">

it works instead.

What's wrong with the assign-statement above?

Re: undefined variable "event"

Your setEvent() works in IE but nof FF. Seems there is no global event in FF.

I'll research it a bit...

Strange how it works when hard coded...

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: undefined variable "event"

Hi Ed,

Tanks for quick response.

I change the code as follows:

if (window.event) { var key = window.event.keyCode; } else { var key = event.which; }

but it fails too.

I was looking into the xajax_core.js code , but I was unable to understand how it works.

It seems, that's the event object will not be passed by ->setEvent or not created, in opposite to if the browser was creating the event via GET request.

And obviously window.event will be always defined by IE, so therefore it works.

Re: undefined variable "event"

In xajax_core.js there is following code:

xajax.events.setEvent = function(command) {
    command.fullName = 'addEvent';
    var element = command.id;
    var sEvent = command.prop;
    var code = command.data;
    if ('string' == typeof element)
        element = xajax.$(element);
    sEvent = xajax.tools.addOnPrefix(sEvent);
    code = xajax.tools.doubleQuotes(code);
    eval('element.' + sEvent + ' = function() { ' + code + '; }');
    return true;
}

/*
    Function: xajax.events.addHandler
    
    Add an event handler to the specified element.
    
    Parameters:
    
    element - (string or object):  The name of, or the element itself
        which will have the event handler assigned.
    sEvent - (string):  The name of the event.
    fun - (string):  The function to be called.
    
    Returns:
    
    true - The operation completed successfully.
*/
xajax.events.addHandler = function(element, sEvent, fun) {
    if (window.addEventListener) {
        xajax.events.addHandler = function(command) {
            command.fullName = 'addHandler';
            var element = command.id;
            var sEvent = command.prop;
            var fun = command.data;
            if ('string' == typeof element)
                element = xajax.$(element);
            sEvent = xajax.tools.stripOnPrefix(sEvent);
            eval('element.addEventListener("' + sEvent + '", ' + fun + ', false);');
            return true;
        }
    } else {
        xajax.events.addHandler = function(command) {
            command.fullName = 'addHandler';
            var element = command.id;
            var sEvent = command.prop;
            var fun = command.data;
            if ('string' == typeof element)
                element = xajax.$(element);
            sEvent = xajax.tools.addOnPrefix(sEvent);
            eval('element.attachEvent("' + sEvent + '", ' + fun + ', false);');
            return true;
        }
    }
    return xajax.events.addHandler(element, sEvent, fun);
}

The function setEvent makes no difference between attackEvent and addEventListener, wondering why it works in IE.

The function addHandler is deprecated, isn't it?

I cannot see nor understand this where the event object will be passed/created (whenever possible in JS) on setEvent.

I suppose if the event will be hardcoded and loaded by GET, the browser creates the event object automatically, whereas xajax doesn't.

Re: undefined variable "event"

Found the bug:

Change the line

eval('element.' + sEvent + ' = function() { ' + code + '; }');

in xajax_core.js (line 274) to

eval('element.' + sEvent + ' = function(event) { ' + code + '; }');

So the 'event' object will be passed correclty.