T3rm1
T3rm1

Reputation: 2582

Problems with Firefox 4 new click event behavior

I have a problem with the behavior of Firefox 4 in regards to parameters passed to the function that is getting called upon a click event.

Take a look at this example:

<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.11/mootools.js"></script>
</head>
<body>
    <span id="e">Klick mich!</span> 
    <script type="text/javascript">
        $("e").addEvent("click", function(a, b, c){
            alert(this);
            alert(a);
            alert(b);
            alert(c);
            console.log(this);
            console.log(a);
            console.log(b);
            console.log(c);
        }.bind(1, [2, 3]));
    </script>   
</body>
</html>

If you open this with Firefox 4 the result is:

  1. 1
  2. 2,3
  3. object MouseEvent
  4. undefined

In any other browser the result is:

  1. 1
  2. 2
  3. 3
  4. undefined

As you can see only Firefox 4 passes an MouseEvent to the function. This behavior breaks a lot of my code.

Do you know any solution? Thanks for help.

EDIT1: Chrome behaves like FF4

Upvotes: 2

Views: 568

Answers (1)

Dimitar Christoff
Dimitar Christoff

Reputation: 26165

the problem is - this is mootools 1.11 - UNSUPPORTED AND OLD

in mootools 1.11, it was accepted to use ( http://docs111.mootools.net/Native/Function.js#Function.bind ):

bind optional, the object that the “this” of the function will refer to.

args optional, the arguments passed. must be an array if arguments > 1

hence, doing .bind(1, [args]) was correct. However, recently the native Function.bind implementation changed in browsers that implement it - https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

thisArg The value to be passed as the this parameter to the target function when the bound function is called. The value is ignored if the bound function is constructed using the new operator.

arg1, arg2, ... Arguments to prepend to arguments provided to the bound function when invoking the target function.

it means, to make it work you need .bind(1,2,3,4); where 1 is bound scope and 2,3,4 are arguments.

you SHOULD upgrade, running mootools 1.11 on browsers that came 4 years after it got written will yield unpredictable results. always. for example, 1.11 won't detect gecko/ff anymore due to deprecated func used to test for it.

what next, check for netscape4? :)

Upvotes: 1

Related Questions