Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5393 (reopened defect)

Opened 2 years ago

Last modified 5 months ago

Global variables are not accessible on load/unload in Firefox

Reported by: kir@maxkir.com Assigned to: sam
Priority: normal Milestone:
Component: Prototype Version:
Severity: normal Keywords: firefox
Cc: mislav, kangax, herr_ernst

Description

hi,

sometimes I get JS error in firebug console like this:

$A is not defined in:

Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}

I suppose, $A function should be moved to the upper section of prototype.js to avoid such errors.

Attachments

dollar-a.diff (2.5 kB) - added by Tobie on 04/24/07 02:58:28.

Change History

06/14/06 19:20:22 changed by Martin Bialasinski

You need to provide a URL where this effect can be observed and steps to reproduce the error.

06/22/06 14:18:00 changed by sp@aps-web.de

Although i cannot provide a reproducable demo for this (not yet) i just want to confirm this "behaviour".

We use the latest Prototype (1.5.0_rc0) / Scriptaculous (1.6.1) Combination.

09/02/06 07:52:44 changed by Romain Baron

It does not seem easy to reproduce, but it occurs quite often (without Scriptaculous too). I guess the bind function is used somewhere inside Prototype itself before $A is defined.

09/06/06 08:49:27 changed by sp@aps-web.de

It seems to me that it often occurs in combination with running AJAX Requests. If a request is not finished yet and you leave or reload the page the message appears.

I've setup a small "barebone" case at http://helpdesk.aps-web.de/undefined_a/ which is only a page which is periodically (every second) requesting the date and time from the server.

The server will need >1s for the ajax answer (the script sleeps 1 second before replying to increase the chance of seeing the error message).

Simply reload the page after a few ajax requests are "in the queue" and you have a VERY GOOD chance to see the message "$A is not defined" in the JS-Debug console of Firefox (Have not seen this error in IE or any other browser so it might be a bug in firefox not finishing AJAX requests timely..?)

09/11/06 14:57:35 changed by markus@fischer.name

I can confirm this behaviour too, and I too think it is related to Ajax requests. The interesting thing is that the errors happens when the page freshly loads, e.g. after I pressed CTRL-F5 for cache bypassed reload. Usually you expect the clean page to not habe errors. Maybe it's FF-centric bug? Cannot test IE with our application here.

10/02/06 22:32:24 changed by mislav

  • type changed from enhancement to defect.
  • severity changed from major to normal.

I can reproduce this from the test case (provided by sp@aps-web.de) after the first refresh (FF2). This seems like a browser bug, although I'm not sure.

My guess is that Firefox continues to make requests while the window unloads in a refresh. Very strange indeed

11/28/06 04:44:04 changed by ngocdaothanh

  • priority changed from normal to low.
  • severity changed from normal to trivial.

We VnRuby use Prototype 1.5.0_rc0 and Scriptaculous 1.6.5 at http://vnruby.org (you can go there to reproduce the error).

When one clicks on one of the 4 links to go to another page, Firebug 0.4.1 (on Firefox 2.0 on Windows XP) sometimes reports: $A is not defined, prototype.js (line 71).

I agree with mislav. My guess is that Firefox continues to process the JavaScript of old page while loading the new page.

11/28/06 14:34:53 changed by mislav

  • cc set to mislav.
  • keywords set to firefox.
  • severity changed from trivial to normal.
  • summary changed from Move $A function to the top of prototype.js to $A is not defined.

Definitely a Firefox bug. I think this is it: 361271.

A workaround (not to show errors) may be possible, but I can't seem to think of one now. Maybe storing the reference to $A somewhere else than on the window object?

03/20/07 12:16:43 changed by mislav

  • status changed from new to closed.
  • resolution set to invalid.

*sigh*

I don't think fixing this is within our range. The bug doesn't break apps, though.

(follow-up: ↓ 11 ) 03/27/07 11:21:22 changed by jfkelley

Please forgive the interjection of a total newbie (I'm not even sure it's appropriate to comment when a ticket has been closed!)

After reading this, I realized I was provoking my own version of this error by trying an Ajax fetch of a signal file on unload (to log the fact that the user left the page).

On thinking about it, I shouldn't be surprised that this fails -- I mean, who's gonna be around to catch the callback response??!!

(in reply to: ↑ 10 ) 03/27/07 11:31:51 changed by mislav

Replying to jfkelley:

Please forgive the interjection of a total newbie (I'm not even sure it's appropriate to comment when a ticket has been closed!)

Comments are always welcome.

After reading this, I realized I was provoking my own version of this error by trying an Ajax fetch of a signal file on unload (to log the fact that the user left the page). On thinking about it, I shouldn't be surprised that this fails -- I mean, who's gonna be around to catch the callback response??!!

Maybe you can try doing it synchronous, but I wouldn't recommend it since it will lag down browsing.

Doing stuff (even crazy hacks) on unload is possible, though. You'll have to investigate.

(in reply to: ↑ description ) 04/20/07 16:26:50 changed by alex-party

  • priority changed from low to high.

Hello, I've found a solution to all your problems, just go to my site and grab the code: http://www.skybyte.net/articles/prototype.js/

04/24/07 02:54:40 changed by Tobie

  • status changed from closed to reopened.
  • resolution deleted.

04/24/07 02:54:57 changed by Tobie

  • summary changed from $A is not defined to [PATCH] $A is not defined.

04/24/07 02:58:28 changed by Tobie

  • attachment dollar-a.diff added.

04/24/07 05:42:44 changed by Tobie

  • status changed from reopened to closed.
  • resolution set to wontfix.

10/22/07 01:04:22 changed by tobie

  • resolution changed from wontfix to fixed.

(In [7986]) prototype: Prevent a Firefox bug from throwing errors on page load/unload. Closes #5393, #9421.

01/11/08 21:37:28 changed by masterleep

  • status changed from closed to reopened.
  • resolution deleted.

This bug still occurs in Prototype 1.6.0.1 and Mac Firefox 2.0.0.11. I get it with 100% reproducibility if there's an Ajax.Request call in a window.unload handler.

For example, load this page, watch the Javascript console, and browse to another page with prototype.js on it:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<title>Test</title>
<body>
  
<script src="javascripts/prototype.js" type="text/javascript"></script>
<script type="text/javascript">
  function unloader()
  {
    new Ajax.Request('/example/page',
    {
       method: 'get',
       parameters: { gone : 1 }
    });
  }

  Event.observe(window, 'load', function() {
    Event.observe(window, 'unload', unloader);
  });
</script>

  <p>Test page</p>
</body>
</html>

01/16/08 18:37:18 changed by masterleep

The skybyte patch does fix the problem - reproducing here in case that site goes away.

In prototype.js, change the bind definition to:

Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    if (typeof $A === 'function')
      return __method.apply(object, args.concat($A(arguments)));
  }
}

01/29/08 02:07:00 changed by MarkD

I did some pretty extensive research on the problem.

It appears that just before the whole page is loaded AND just before unload Firebug randomly(?) loses the scope of global functions / Objects. In other words: it doesn't know that "$A" or "bindAsEventListener" or whichever function belongs to the window object.

It sometimes also loses methods / props within objects. For example 'Event.observers'. Event is defined but Event.observers is not.

Even it's own console.log doesn't work from time to time.

You can easily test it:

Function.prototype.bind = function() {
  var __method = this, args = window.$A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat(window.$A(arguments)));
  }
}

should be working like a charm.

The above workaround is just a patch. Problems will occur in different places like document.defaultView.getComputedStyle, unloadCache, anywhere where a global reference is used without 'window.'.

It is almost impossible and not worthy patching the entire framework. And as you figured it is not a prototype only problem, any script (specially with mouseovers on links) suffers from it.

I consider it a firebug problem, a little flaw in this lovely tool, too bad.

01/29/08 02:27:10 changed by mislav

Some of us still believe that this is a Firefox problem related to bad unloading. It doesn't affect the apps, though. Annoying as it may be, I don't think that anyone found it harmful.

Firefox bug tracker could be a source of more info.

01/29/08 05:14:12 changed by masterleep

I was able to reproduce the error on a machine without Firebug, so it doesn't appear to be anything to do with that tool.

01/29/08 05:25:05 changed by masterleep

I'm speculating here, but is this occuring because the wrong page is handling the Ajax response (ie, the new one rather than the old)? If so, can you abort the response in general once this condition is recognized, as a single place to patch the problem rather than having to patch all over the place?

01/29/08 09:13:33 changed by MarkD

I checked on a FF without firebug installed too, and yes, I can reproduce it, which makes the problem a lot worse of course.

In my script there aren't any Ajax calls. It's just a simple hover / unhover with Scriptaculous Effect.Highlight. It lo

I will post a link to the simplified version of the script soon.

01/29/08 13:11:12 changed by Tobie

  • priority changed from high to normal.
  • summary changed from [PATCH] $A is not defined to Global variables are not accessible on load/unload in Firefox.

01/29/08 13:12:06 changed by Tobie

MarkD: with which version of Prototype is this an issue ?

01/29/08 14:04:03 changed by MarkD

It is Prototype version 1.5.1.1 although I really doubt if the version matters.

01/29/08 17:04:32 changed by masterleep

The problem still occurs in Prototype 1.6.

01/30/08 02:39:12 changed by Tobie

MarkD: yes it does, the hover in/out problem should be corrected in Proto 1.6 afaik.

01/31/08 19:15:20 changed by kangax

  • cc changed from mislav to mislav, kangax.

03/10/08 18:25:06 changed by herr_ernst

  • cc changed from mislav, kangax to mislav, kangax, herr_ernst.

03/10/08 18:50:34 changed by herr_ernst

If you make an Ajax-call in window.onunload (like I'm doing, because I want to save the settings of the user when leaving the site), you should really make a synchronous call, even it blocks the browser for the time of the request. Otherwise there will be errors on the next page.

(follow-up: ↓ 35 ) 03/14/08 21:14:45 changed by reenhanced

Okay, strange. I'm getting this error in combination with ModalBox, but only if I pass it a certain string to display.

I can't figure this out for the life of me, but it's breaking my app (modalbox just doesn't appear).

firefox 2.0 with firebug (occurs with it disabled as well)

03/14/08 21:15:28 changed by reenhanced

Also occurs on firefox 3 beta 4

03/14/08 21:16:44 changed by reenhanced

And IE7

(in reply to: ↑ 32 ) 03/14/08 21:20:42 changed by reenhanced

I've resolved it.. looks to have been related to attempting to display an unordered list. Weirdest thing I've ever seen...

04/01/08 11:44:15 changed by kangax

Does this still happen with 1.6.0.2?

04/05/08 04:23:38 changed by jdalton

may be related to #9211

04/07/08 13:31:15 changed by rbaarsma

This problem is still reproducable with Firefox 2.0.0.13 and Prototype 1.6.0.2 when using Ajax.Request on a slow connection and making multiple requests at the same time.

04/07/08 13:36:36 changed by reenhanced

It appears that my issue was unrelated to this. If possible, please remove my comments.

I apologize for the misleading information.