views:

169

answers:

2

I'm getting a JS error because my $(function () {...}) handler is being fired seemingly before the prerequisite plugin script is loaded. Only happens in IE (testing in IE7).

I have some HTML in my <head> that looks like this:

<script type="text/javascript" src="../resources/org.wicketstuff.jwicket.JQuery/jquery-1.4.2-special.js"></script>
...
<script type="text/javascript" id="noConflict"><!--/*--><![CDATA[/*><!--*/
jQuery.noConflict();
/*-->]]>*/</script>
...
<script type="text/javascript" src="../resources/com.csc.aims.wicket.components.collapsiblefieldset.CollapsibleFieldsetBehavior/jquery.collapsiblefieldset.js"></script>
<link rel="stylesheet" type="text/css" href="../resources/com.csc.aims.wicket.components.collapsiblefieldset.CollapsibleFieldsetBehavior/jquery.collapsiblefieldset.css" />
<script type="text/javascript">
jQuery(function(){
jQuery('#collapse119').collapse({"iconClosedUrl":"../resources/img/white_plus","iconOpenUrl":"../resources/img/white_minus"});
});
</script>

So notice the sequence, according to the HTML code, is as follows:

  1. jquery-1.4.2-special.js
  2. jQuery.noConflict() call
  3. jquery.collapsiblefieldset.js //defines $.fn.collapse
  4. jQuery('#collapse119').collapse(...) is called

When this code runs in FF, everything works fine. When I test it in IE7 (or IE8 w/Compat. View: IE7 standards mode), I get a javascript error. The debugger shows me that jQuery.fn.collapse is undefined.

Using the IE8 developer tools, I try to look at jquery.collapsiblefieldset.js. I see the script in the list, but the tool tells me that I can't set a breakpoint because the script isn't loaded.

Why does the collapsiblefieldset.js not get loaded before my $() ready handler is run? Any insight would be appreciated! Thanks.

+1  A: 

Put all of your scripts at the bottom of the page, right before the </body> tag.

If that doesn't fix it, move the scripts that do not appear to be loading in time back to the <head>, and leave the rest of the scripts at the bottom.

Robert Harvey
The web framework I'm using doesn't easily permit me to put it at the bottom of <body>. I changed it so that the collapsiblefieldset.js is in <head>, and the jQuery ready handler is down in the body, at the point where the widget is defined. This doesn't seem to fix the problem.
RMorrisey
Also, there are a lot of other scripts that run on this page; the most expensive of which happen in $() ready handlers. Not sure if this is relevant.
RMorrisey
Looks like you're going to have to use the developer tools in IE8 to try and debug the Javascript code. http://coolwebdeveloper.com/2009/03/fantastic-new-javascript-debugging-tool-with-ie-8-and-its-list-of-features-hard-to-live-without/
Robert Harvey
I did use the IE8 debugger, as mentioned in the original post. =)
RMorrisey
+3  A: 

You're using

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

which is synonym of

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

Instead, you might try

$(window).load( function(){...} );

which fires later in the page loading sequence.

Ken Redler
Thank you! This seems promising; I'll try it out on Monday
RMorrisey
I was mistaken to think that it was a timing issue. The errors I got were masking the actual problem, which was a syntax error in the external .js file in question. Assigning the command script to the window load event seems to have exposed the underlying problem.
RMorrisey