jQuery: The Write Less, Do More JavaScript Library

$.getScript in Firefox 3

View: New views
10 Messages — Rating Filter:   Alert me  

$.getScript in Firefox 3

by robert_shipley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I've been developing a website and we use getScript to load all of our
javascript files (other than jQuery) so the header of the page is
shorter. We have a mechanism in place which allows functions to be
executed when the script has downloaded. (Registering a function as a
dependency of a file and when that file has been downloaded the
callback from the getScript triggers the dependent code)

This all works fine in IE and Firefox 2.5. I recently installed the
new version of Firefox (3) and am experiencing an unexpected behavior.
It appears that the callback function from a getScript call is
executing before the downloaded script has been run.

For example: I use get script to download a script containing
"function 1". I have a piece of code in another script depending on
"function 1", so it gets registered in our listener functionality. The
callback from the getScript call for downloading "function 1" triggers
the running of the dependent code. This all works in IE and Firefox
2.5 but in Firefox 3 the dependent code is erroring because it can not
find "function 1".

If I place a delay into the dependent code (1 sec), "function 1" seems
to be available and no errors occur.

There appears to be an early triggering of the callback in Firefox 3.
I believe this is a slight browser incompatibility in the new version
of Firefox (3).

Has any one experienced this? Is this a known issue? Can a fix be
created as it's stopping everything from working for us.

Thanks in advance to any one who can offer any help on this!!!

Re: $.getScript in Firefox 3

by robert_shipley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Any one got any ideas??

On Jun 25, 10:12 am, robert_shipley <robert_ship...@...>
wrote:

> I've been developing a website and we usegetScriptto load all of our
> javascript files (other than jQuery) so the header of the page is
> shorter. We have a mechanism in place which allows functions to be
> executed when the script has downloaded. (Registering a function as a
> dependency of a file and when that file has been downloaded the
> callback from thegetScripttriggers the dependent code)
>
> This all works fine in IE and Firefox 2.5. I recently installed the
> new version of Firefox (3) and am experiencing an unexpected behavior.
> It appears that the callback function from agetScriptcall is
> executing before the downloaded script has been run.
>
> For example: I use get script to download a script containing
> "function 1". I have a piece of code in another script depending on
> "function 1", so it gets registered in our listener functionality. The
> callback from thegetScriptcall for downloading "function 1" triggers
> the running of the dependent code. This all works in IE and Firefox
> 2.5 but in Firefox 3 the dependent code is erroring because it can not
> find "function 1".
>
> If I place a delay into the dependent code (1 sec), "function 1" seems
> to be available and no errors occur.
>
> There appears to be an early triggering of the callback in Firefox 3.
> I believe this is a slight browser incompatibility in the new version
> of Firefox (3).
>
> Has any one experienced this? Is this a known issue? Can a fix be
> created as it's stopping everything from working for us.
>
> Thanks in advance to any one who can offer any help on this!!!

Parent Message unknown Re: $.getScript in Firefox 3

by Mark T-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.  If I get time I will
try and recreate the problem using a simpler setup, but as of now I
haven't got the spare time to do this.

I have, however, been doing some investigation and here are my
findings:

 - 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 ("exec
complete") with getScript and one in the callback ("callback
triggered") which is triggered on (apparent) completion of the load.

Most of the time, these appear in the console in the correct order
("exec complete" => "callback triggered").  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.

Parent Message unknown Re: $.getScript in Firefox 3

by Mark T-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.  If I get time I will
try and recreate the problem using a simpler setup, but as of now I
haven't got the spare time to do this.

I have, however, been doing some investigation and here are my
findings:

 - 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 ("exec
complete") with getScript and one in the callback ("callback
triggered") which is triggered on (apparent) completion of the load.

Most of the time, these appear in the console in the correct order
("exec complete" => "callback triggered").  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.

Parent Message unknown Re: $.getScript in Firefox 3

by robert_shipley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.

Re: $.getScript in Firefox 3

by Mark T-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I'm getting even more convinced that this is an issue with FF3 &
jQuery.  Interestingly, it only affects local JS files, which are
inserted as text into the head of the document between script tags.

Remote files, which are loaded using <script src=...> and monitored
using onload / onreadystate execute fine.

Why is it that local files are not loaded using the src attribute of
the script tag too?

Mark.

On Sep 16, 11:57 am, robert_shipley <robert_ship...@...>
wrote:

> 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.

Re: $.getScript in Firefox 3

by Mark T-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Ok, my temporary fix has been to alter the following block of code
from line 2656 in jquery 1.2.6:

                // If we're requesting a remote document
                // and trying to load JSON or Script with a GET
                if ( s.dataType == "script" && type == "GET"){
                                //&& remote.test(s.url) && remote.exec(s.url)[1] != location.host )
{
                        var head = document.getElementsByTagName("head")[0];
                        var script = document.createElement("script");

I have commented out the second part of the IF statement which tests
whether the URL is remote.  This will force all scripts to use the
<script src=??? and onreadystatechange / onload events whether they
are remote or local.

I'd be interested in hearing why using the alternative way for local
scripts is better.

Cheers,

Mark.

On Sep 16, 11:57 am, robert_shipley <robert_ship...@...>
wrote:

> 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.

Re: $.getScript in Firefox 3

by Mark T-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hmm, actually I think I'm still getting the problem, it's just being
quiet about it now :(

On Sep 16, 3:00 pm, Mark T <mtu...@...> wrote:

> Ok, my temporary fix has been to alter the following block of code
> from line 2656 in jquery 1.2.6:
>
>                 // If we're requesting a remote document
>                 // and trying to load JSON or Script with a GET
>                 if ( s.dataType == "script" && type == "GET"){
>                                 //&& remote.test(s.url) && remote.exec(s.url)[1] != location.host )
> {
>                         var head = document.getElementsByTagName("head")[0];
>                         var script = document.createElement("script");
>
> I have commented out the second part of the IF statement which tests
> whether the URL is remote.  This will force all scripts to use the
> <script src=??? and onreadystatechange / onload events whether they
> are remote or local.
>
> I'd be interested in hearing why using the alternative way for local
> scripts is better.
>
> Cheers,
>
> Mark.
>
> On Sep 16, 11:57 am, robert_shipley <robert_ship...@...>
> wrote:
>
> > 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.

Re: $.getScript in Firefox 3

by Mark T-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Actually, scratch that - it seems to be working now.

I'd still appreciate a comment from a jQuery guru on the original
problem I've worked around here!

Mark.

Re: $.getScript in Firefox 3

by robert_shipley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Anyone got any further ideas on this one?

Rob