jQuery: The Write Less, Do More JavaScript Library

 « Return to Thread: $.getScript in Firefox 3

Re: $.getScript in Firefox 3

by robert_shipley :: Rate this Message:

Reply to Author | View in Thread


Posted by Rob on Behalf of Mark...

I've been doing a little investigation into this (I'm a colleague of
Rob's) and we are still experiencing the issue in Firefox 3.  I'm
afraid it's very difficult for me to produce an example of this as we
are using quite a complex set of getScript calls in our website and
can't make this available to the outside world.  I've managed to
generate a replica of the page here: http://www.tcc-net.com/jquery-getscript/

Here are my findings todate:

 - We are calling getScript to retrieve local javascript files.
 - As soon as the file is loaded, we attempt to execute a function
within the script.

I put a few console.log messages into the files (Firebug uses
console.log for debugging) to output messages at certain points.  The
key ones were a message at the end of the file being retrieved ("end
of tablesorter") with getScript and one in the callback ("would
trigger") which is triggered on (apparent) completion of the load.

Most of the time, these appear in the console in the correct order
("end of tablesorter" => "would trigger").  Occasionally, however,
they appear the alternate way around and this is when the error
(myMethod() is not a valid function, or words to those effect) is
seen.

I therefore deduce that the callback is being triggered before the
execution of the javascript is complete (even though it is loaded, it
has not executed fully).  It is down to a race as to whether the
callback is triggered first or the script completes execution.

Looking at the jQuery code, and I am claiming no expertise here so I
may well be wrong, it seems that the "globalEval" function is used to
execute a local script once downloaded.  I do not see anywhere in this
function, however, where there are any checks on whether the script
has fully executed.

It might be that in IE6, IE7, FF < 3 the execution of the javascript
is paused until the inserted script element has been executed, and in
FF3, no such pause occurs.  I am hypothesising here though, and really
don't have enough information on how this method of script loading
works internally.

I'm posting this information here more to see if it triggers any
thoughts in those more experienced jQuery contributors.  In the
meantime, I will continue my own investigations.

Cheers,

Mark.

 « Return to Thread: $.getScript in Firefox 3