Hi Ryan,

The reason you are having problems is likely due to the way jQuery
determines DOM readiness. As noted above, if functions are passed to $
() or $(document).ready() after the DOM has loaded, they are executed
immediately. This produces the behavior you are expecting. But when
jQuery loads, it listens for native events (DOMContentLoaded, load,
etc) to determine readiness. If jQuery is injected into the page after
these events fire, I doubt the ready state is reliable.

This is reproducable with a blank page and firebug. If you create an
HTML page and open it in firebug, create  a script node and then
append it to the head, you can see that jQuery is defined, but
jQuery.isReady evaluates to false. Because the "load" and
"DOMContentLoaded" events have already fired, the logic breaks. Note
that all other bindings should work as expected. Hope that helps.

On Oct 8, 8:11 am, Ryan Crumley <crum...@gmail.com> wrote:
> Rob,
>
> Point taken about using onLoad for a script tag possibly not being
> supported. In this case however that part of the code is working fine.
> The issue I am having is related to jQuery being added to a page after
> the page has completed loading. When this happens ready listeners are
> never executed. If jQuery is present when the page loads then ready
> listeners are executed as expected.
>
> Ryan
>
> On Oct 8, 12:45 am, RobG <robg...@gmail.com> wrote:
>
>
>
> > On Oct 8, 10:04 am, Ryan Crumley <crum...@gmail.com> wrote:
>
> > > I am experiencing unexpected behavior using $(document).ready() on a
> > > page where I inject jQuery after the page has loaded and then attach
> > > listeners to the ready event. The ready() event is never called using
> > > Firefox and Safari however it is called using IE.
> > [...]
> > >                 function loadjquery() {
> > >                         var url = 
> > > "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/
> > > jquery.js";
> > >                         var head = 
> > > document.getElementsByTagName("head")[0], done = false;
> > >                         var script = document.createElement("script");
> > >                         script.src = url;
>
> > >                         script.onload = script.onreadystatechange = 
> > > function() {
>
> > There is no onload attribute for script elements defined in HTML 4.01,
> > therefore you should not expect that a script element will fire a load
> > event. The fact that some browsers do and some don't should be enough
> > to tell you that browser behaviour is inconsistent.  You are unlikely
> > to be able to reliably fix that with script.
>
> > If you want a reliable indicator that a script has finished loading
> > and is ready for use, put a statement at the bottom like:
>
> >   var SCRIPT_LOADED = true;
>
> > --
> > Rob

Reply via email to