IE7 jQuery (document) .ready () problem

I have a page that works fine in Firefox, but throws an error in IE. I load jQuery dynamically (if it is not already loaded) and then do some things in the jQuery (document) .ready () block. However, IE throws a terrible "Expected Object" error when it hits the jQuery (document) .ready () block.

You can view the full page code here: http://www.pastie.org/977767

IE throws an error right in jQuery (document) .ready ().

Any ideas on what's going on here? Again, this works great in Firefox. It seems like IE thinks jQuery is loaded, but it is not, or is jQuery still loading when the jQuery (document) .ready () block is encountered?

+6
jquery
source share
2 answers

When you add a script to the document, it loads asynchronously. In IE, the following script ...

try{ jQuery(document).ready(function() { jQuery.getScript("/CalendarViewer/js/utils.js", function(){ jQuery.getScript("/CalendarViewer/js/groupcatselector.js", function(){ jQuery.getScript("/CalendarViewer/js/calendarportlet.js", function(){ jQuery.getScript("/CalendarViewer/js/calendarportletmain.js", function(){ var cpm = calendarportletmain; cpm.doEditDefaults("V_7f208bca412b42a68c19eb104bf46f14", "/CalendarViewer", groupCats_V_7f208bca412b42a68c19eb104bf46f14); }); }); }); }); }); }catch(err){ alert("error in view.jsp="+err.number+" "+err.description); } 

... is parsed and executed before IE has finished loading and parsing the jQuery script. This may not be the case in Firefox, if Firefox has already cached the script, it does not take time to download and can be analyzed immediately. This could make parsers work differently, Firefox parsed the script as soon as it was loaded, and IE checked the parsing until the thread became idle.

You can move this code to the end of the setUpJquery function, which means that it will only execute if there is a jQuery object. Alternatively, you can put the code in your own function and call this function from the setUpJquery function.

+2
source share

How I got around this:

 $(document).ready( function () { document_init(); }); function document_init() { try{ checkDivLoaded(); [ ... do more stuff ... ] } catch( err ) { setTimeout('document_init()', 200); } } function checkDivLoaded() { if ( $('#targetDiv').length == 0) $jquery.error( 'not ready' ); } 

This is not very good, but it works ... it works in several files, in every browser that I can think of (which I care about), and means that you do not need to mess with the source code of the parent page. This way you can keep clear marks.

+1
source share

All Articles