Side navigation
#2509 closed bug (fixed)
Opened March 14, 2008 11:58AM UTC
Closed July 11, 2010 07:04PM UTC
Last modified March 15, 2012 01:50AM UTC
$().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
Attachments (0)
Change History (8)
Changed March 17, 2008 10:38PM UTC by comment:1
Changed April 01, 2008 05:29PM UTC by comment:2
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.
Changed April 02, 2008 03:16AM UTC by comment:3
I like that. It leaves a gap.. but it's a smart option.
Changed April 05, 2008 06:11PM UTC by comment:4
Neither of these options will work if jQuery is itself loaded after the window.load event has fired (ex: via ajax).
Changed April 10, 2008 03:31PM UTC by comment:5
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.
Changed April 22, 2008 06:08AM UTC by comment:6
component: | core → event |
---|
Changed January 17, 2009 07:18PM UTC by comment:7
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.
Changed July 11, 2010 07:04PM UTC by comment:8
resolution: | → fixed |
---|---|
status: | new → closed |
Fixed in 1.4.
The easier (internal) solution is to avoid making bindReady() a "lazy" function and just call it right away when declaring jQuery.