Bug Tracker

Opened 11 years ago

Closed 9 years ago

Last modified 7 years ago

#2509 closed bug (fixed)

$().ready() functions never fire if added after document loads

Reported by: giulio Owned by:
Priority: major Milestone: 1.2.4
Component: event Version: 1.2.3
Keywords: Cc:
Blocked by: Blocking:

Description

For Full detail see discussion here: http://groups.google.com/group/jquery-dev/browse_thread/thread/c713fa86d4a28785

First Post:

Hi,

We have spent some time racking our heads why $(document).ready() wasn't always firing and we finally found it.

The ready() function checks jQuery.isReady. If true, it fires straight away, if false, it gets queued and fired once the document is ready.

This is fine if you have a page which registers a ready function on the initial load of the page. At this point jQuery hooks itself up to capture the documents ready event, fires your function and also sets jQuery.isReady to true.

However, if you have a page with jQuery on it and don't register anything on the first load, then jQuery.isReady is left to be false. Then you load a script via ajax which calls $ (document).ready(functrion(){/*do stuff*/}) but it won't fire. Why? because, the page is already loaded, so the document ready event doesn't fire and hence doesn't set jQuery.isReady to true. Registering a function at this point get's added to jQuery.readyList but it is never run.

For now I have fixed this by appending this line to the end of the jQuery file which registers a dummy function.

$(document).ready(function(){});

This function does nothing other than make sure jQuery hooked up the ready event in the case that no ready function was externally queued on jQuery.readyList on the initial page load. There may be a better fix for this by a jQuery guru, buth this does the trick for now.

Giulio

Change History (8)

comment:1 Changed 11 years ago by flesler

The easier (internal) solution is to avoid making bindReady() a "lazy" function and just call it right away when declaring jQuery.

comment:2 Changed 11 years ago by joern

How about making the document ready via window.load when document.ready isn't used? That could fix the issue while avoiding the overhead of document.ready when it isn't needed.

comment:3 Changed 11 years ago by flesler

I like that. It leaves a gap.. but it's a smart option.

comment:4 Changed 11 years ago by sunsean

Neither of these options will work if jQuery is itself loaded after the window.load event has fired (ex: via ajax).

comment:5 Changed 11 years ago by flesler

Right, it will still fail. This would happen to any event-based script. If you want to preload an image, that is already preloaded, and you get to bind 'load' too late, it'd not work either.

I think we could just tell users to set jQuery.isReady to true right after loading jQuery.

comment:6 Changed 11 years ago by brandon

Component: coreevent

comment:7 Changed 11 years ago by dmethvin

Summary: $(document).ready() - $.isReady bug$().ready() functions never fire if added after document loads

I've updated the title, and closed #3185 which was a dup of this one.

comment:10 Changed 9 years ago by dmethvin

Resolution: fixed
Status: newclosed

Fixed in 1.4.

Note: See TracTickets for help on using tickets.