Side navigation
#904 closed bug (fixed)
Opened January 31, 2007 07:47PM UTC
Closed December 16, 2007 09:41PM UTC
DOM ready handlers will never be executed if jQuery is dynamically loaded
Reported by: | Arrix | Owned by: | john |
---|---|---|---|
Priority: | minor | Milestone: | 1.2.2 |
Component: | event | Version: | 1.1.3 |
Keywords: | domReady ready | Cc: | |
Blocked by: | Blocking: |
Description
If jquery.js is dynamically loaded into a page, document ready will never be triggered.
<script type="text/javascript"> window.onload = function() { var s = document.createElement('script'); s.type = 'text/javascript'; s.src = 'jquery.js'; document.getElementsByTagName('head')[0].appendChild(s); document.onclick = function() { alert($.isReady); } }; </script>
IE, Firefox and Opera will alert "false" after you click on the page. Haven't tested in Safari yet.
Attachments (0)
Change History (7)
Changed January 31, 2007 09:22PM UTC by comment:1
Changed March 24, 2007 06:09PM UTC by comment:2
milestone: | → 1.1.3 |
---|---|
need: | → Test Case |
Changed July 21, 2007 02:37PM UTC by comment:3
description: | If jquery.js is dynamically loaded into a page, document ready will never be triggered.\ {{{\ <script type="text/javascript">\ window.onload = function() {\ var s = document.createElement('script');\ s.type = 'text/javascript';\ s.src = 'jquery.js';\ document.getElementsByTagName('head')[0].appendChild(s);\ \ document.onclick = function() {\ alert($.isReady);\ }\ };\ </script>\ }}}\ \ IE, Firefox and Opera will alert "false" after you click on the page. Haven't tested in Safari yet. → If jquery.js is dynamically loaded into a page, document ready will never be triggered. \ {{{ \ <script type="text/javascript"> \ window.onload = function() { \ var s = document.createElement('script'); \ s.type = 'text/javascript'; \ s.src = 'jquery.js'; \ document.getElementsByTagName('head')[0].appendChild(s); \ \ document.onclick = function() { \ alert($.isReady); \ } \ }; \ </script> \ }}} \ \ IE, Firefox and Opera will alert "false" after you click on the page. Haven't tested in Safari yet. |
---|---|
milestone: | 1.1.3 → 1.1.4 |
owner: | → john |
version: | 1.1 → 1.1.3 |
I think I've got an idea on how this might work. When jQuery is loaded, it traverses down to see what the last element in the document is, if it's a script element, then we're being loaded naturally. If it's not, then we're being loaded dynamically (and thus the document 'is ready').
Changed July 21, 2007 02:43PM UTC by comment:4
Humm... that won't work, though, if the last element on the page is, in fact, a script element. For example:
document.body.appendChild(script);
Lame.
Changed September 26, 2007 07:58AM UTC by comment:5
When jQuery loads, we can first check whether the document is ready. If so, mark jQuery.isReady true.
John has discovered great new techniques to check whether the document is ready :-)
"If you attempt to insert into the
document.body before the document is fully loaded, an exception is
thrown. I take advantage of that to determine when the document is
fully loaded."
WOW, we can now hopefully fix this!
Changed September 27, 2007 10:06PM UTC by comment:6
@arrix: Unfortunately, that only works in IE - and only sometimes. There's ways to determine if the DOM is ready in IE, Safari, and Opera - all using document.readyState. Unfortunately, Firefox doesn't provide this property, so an alternative hack will have to be found.
Changed December 16, 2007 09:41PM UTC by comment:7
milestone: | 1.1.4 → 1.2.2 |
---|---|
resolution: | → fixed |
status: | new → closed |
As of Rev [4162] you can manually trigger the ready code. So once jQuery and your scripts are loaded you can run this line to trigger the ready event.
$(document).trigger("ready");
I believe this is about as fixed as this will get for now.
How can a script detect how it was loaded?