Bug 15505 - onload sometimes fired before all resources are loaded
: onload sometimes fired before all resources are loaded
Status: UNCONFIRMED
: WebKit
New Bugs
: 523.x (Safari 3)
: Macintosh Intel Mac OS X 10.4
: P2 Normal
Assigned To:
: http://openlayers.org/dev/examples/
:
:
: 10403
  Show dependency treegraph
 
Reported: 2007-10-14 07:29 PST by
Modified: 2012-01-22 16:25 PST (History)


Attachments


Note

You need to log in before you can comment on or make changes to this bug.


Description From 2007-10-14 07:29:19 PST
Version 522.12.1, Safari 3.0.3

The above URL is a collection of examples of the OpenLayers Javascript library. In the development version of OpenLayers, Javascript source files are loaded by the following mechanism:

        var allScriptTags = "";
        var host = OpenLayers._getScriptLocation() + "lib/";

        for (var i = 0; i < jsfiles.length; i++) {
            if (/MSIE/.test(navigator.userAgent) || /Safari/.test(navigator.userAgent)) {
                var currentScriptTag = "<script src='" + host + jsfiles[i] + "'></script>";
                allScriptTags += currentScriptTag;
            } else {
                var s = document.createElement("script");
                s.src = host + jsfiles[i];
                var h = document.getElementsByTagName("head").length ?
                           document.getElementsByTagName("head")[0] :
                           document.body;
                h.appendChild(s);
            }
        }
        if (allScriptTags) document.write(allScriptTags);

Occasionally, when loading an example in Safari, the page will fail to load, reporting an error:

Value undefined (result of expression OpenLayers.Map) is not an object. Cannot be used with new.
http://openlayers.org/dev/examples/example.html

This error generally indicates that the Javascript file containing the definition for OpenLayers.Map is not yet available.

I've been able to get this to happen most regularly with the example.html example: http://openlayers.org/dev/examples/example.html

The first javascript call is from a file which is loaded first: var header = OpenLayers.Util.getElement("browserHeader");

But the OpenLayers.Map class, which is loaded later, is not available.

All actions are taking place on 'onload' (not before). 

The only explanation I can think of for this is that onload is firing before all the included script resources -- which are written into the document via document.write() -- are available. 

After causing the error to happen, refreshing the page seems to cause it to always work. 

This may be merely anecdotal, but it seems to happen more often if I manually enter the URL into the URL entry form, rather than following a link.

I was not able to reproduce this bug when running against a locally checked out copy of OpenLayers (file:// URLs).

I believe the Safari-specific hack in the above loading code was added in Safari2, and to the best of my knowledge, it worked relatively reliably there, because appending elements to the DOM suffered the same problems as we are seeing now. I have not confirmed whether switching to the non-Safari specific method fixes this problem in Safari3.

This bug is not easily reproducible, insofar as it happens sometimes, but doing the exact same thing, won't happen other times. It seems to happen most of tne the first time I view a page (with the OpenLayers Library in it.)
------- Comment #1 From 2007-10-15 09:58:06 PST -------
(In reply to comment #0)
> I believe the Safari-specific hack in the above loading code was added in
> Safari2, and to the best of my knowledge, it worked relatively reliably there,
> because appending elements to the DOM suffered the same problems as we are
> seeing now. I have not confirmed whether switching to the non-Safari specific
> method fixes this problem in Safari3.

This approach needs to be tried as well.
------- Comment #2 From 2008-12-02 10:17:20 PST -------
See also Bug 17761.