• Last Modified
  • 26.11.2007

Prototype.js $A Fix

Problem Description: When applying Events to elements or window, Firefox throws up an error if page is still loading:

Error: $A is not defined

Source File: prototype.js Line: 79

I've been looking all over the Internet to find a solution, but it appears this bug was in prototype for a while and I could not find a fix for a long time.

After all, I've decided to look into code myself. First I moved declaration of $A function in front of the code - no luck.

Finally, after some thinking, I applied a patch, that WILL NOT ALLOW executing $A function if it's not initialized.

And it worked just fine !!! Although, I can't explain the origin of this problem, had no time to dig further.

Just grab the code and forget this problem ever existed :)

Prototype.js v 1.5.0/1.5.1 fix:

Function.prototype.bindAsEventListener = function(object) {
  var __method = this, args = $A(arguments), object = args.shift();
  return function(event) {
      if(typeof $A === 'function'){                  // <-- Added Firefox Fix
         return __method.apply(object, [( event || window.event)].concat(args).concat($A(arguments)));
      }
  }
}

// "Thanks to Dave for the hint!"
Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
      if(typeof $A === 'function'){                  // <-- Added Firefox Fix
         return __method.apply(object, args.concat($A(arguments)));
      }
  }
}

Prototype.js v 1.6.0+ fix:

  bind: function() {
    if (arguments.length < 2 && arguments[0] === undefined) return this;
    var __method = this, args = $A(arguments), object = args.shift();
    return function() {
	   if(typeof $A === 'function'){                  // <-- Added Firefox Fix
	     return __method.apply(object, args.concat($A(arguments)));
	   }	
    }
  },

  bindAsEventListener: function() {
    var __method = this, args = $A(arguments), object = args.shift();
    return function(event) {
	  if(typeof $A === 'function'){                  // <-- Added Firefox Fix
	     return __method.apply(object, [event || window.event].concat(args));
	  }
    }
  },
  • Bookmark this page on Delicious
  • Bookmark this page on StumbleUpon
  • Digg this page on Digg
  • Submit this page on Reddit
  • Submit this page on Furl
  • Share this page on Facebook
  • Bookmark this page on Google
  • Bookmark this page on Yahoo
  • Search for links to this page on Technorati
  • Search for links to this page on IceRocket

Comments

2007-04-08
Dave
Works a treat. Added the same fix for Function.prototype.bind as well. Thanks!

2007-04-02
Levi
Perfect. Works like a charm!

2007-04-03
Srinivas
Hi Thanks a lot. This is what i am looking for.It solved my issue.

2007-05-07
Nicky
Thanks, it helped with my web application.

2007-05-02
Srinivas Thumam
Thanks for the solution. But identical operator ie ("===") is not working in firefox so I replaced with equal to ("==") in place of ("==="). Anyway thanks once again.

2007-06-03
Alex, replying
It appears that mr. Thumam was testing in Firefox 1.7. I've tested it with both operators - works fine, must be something else. Actually, I've run this piece of code through www.jslint.com and it suggested me to use === operator. Also, Mr. Douglas Crockford at www.crockford.com, an expert in Javascript , recommends to use === operator, which works faster (said in his Javascript tutorial)

2007-06-03
RETFU
Same problem for me with : Element.extend.cache Thanks for the fix ;)

2007-06-08
mitch
actually, i'm using v1.5.1, and *still* seeing that issue in FF2.0.0.4. i applied your patch, and the error's gone. thanks a bunch.

2007-06-12
Ken Snyder
I'm also using Prototype v1.5.1 and seeing this in FF 2.0.0.4. Thanks for the fix! I can reproduce it for any Scriptaculous effect that is running when loading a new page (i.e. a link is clicked).

2007-09-02
NaBaL
Strange that the bug fix is on this line and not in the 'args = $A(arguments)' code which also use $A function. But it works. As it is a bug that doesn't cause other errors on the page, I'd rather let the prototype.js copyright code as it is now than make this change.

2007-11-10
Logamurugan
This is wat i need. Thanks a lot :)

2007-11-12
Diona Kidd
Thanks, this is just what I needed too. Has this bug been reported to the Prototype project?

2007-12-08
Fabrizio
I am using Linux with Firefox 2.0 and the errors keeps on being displayed

2007-12-11
Fabrizio
I am using Prototype version 1.6 the website in object is dev.timeet.com

2007-12-06
Oncle Tom
Extra thanks, I was wondering where the error came from.

2008-01-10
rakesh
Thanks,Iwas trying to find answer,thanks again

2008-02-09
Jo
Sweet thanks - that fixed my problem :)

2008-03-10
wshen
thanks, you save me a day

2008-05-12
Druss
Thanks, i will try !

2008-06-11
William Sanches
Alex and Druss. I'm using prototype 1.6 too and my fix goes: For "bind: function()" -> on the line 205 I've put the fix on the line 209 For "bindAsEventListener: function()" -> on the line 215 I've put the fix on the line 216 The error disappears but something still strange. Sometimes my AJAX function returns the response and sometimes no. I'm looking for a possible error in my code. Anyway, the fix works!

2008-11-07
keemor
Why isn't this fix in prototype 1.6.0.3 yet?

2008-11-11
Alex, replying to keemor
I would not know about it, haven't had the chance to ask the prototype author, but I might when I'm back from NY next week.

2008-11-06
Rosina
i love this site.S

2008-11-11
Timmy
huyak


Leave a comment (E-mail address never displayed)





2006-2007 @ SkyByte.net. All Rights Reserved xhtml 1.0 css