Bug 15505 - onload sometimes fired before all resources are loaded
Summary: onload sometimes fired before all resources are loaded
Status: UNCONFIRMED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: 523.x (Safari 3)
Hardware: Mac (Intel) OS X 10.4
: P2 Normal
Assignee: Nobody
URL: http://openlayers.org/dev/examples/
Keywords:
Depends on:
Blocks: 10403
  Show dependency treegraph
 
Reported: 2007-10-14 07:29 PDT by Christopher Schmidt
Modified: 2022-07-22 11:18 PDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christopher Schmidt 2007-10-14 07:29:19 PDT
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 David Kilzer (:ddkilzer) 2007-10-15 09:58:06 PDT
(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 David Kilzer (:ddkilzer) 2008-12-02 10:17:20 PST
See also Bug 17761.