Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

javascript - jQuery $(document).ready() failing in IE6

I have the following code:

// Creates a timer to check for elements popping into the dom            
timer = setInterval(function ()
{          
    for (p in pixelTypes)
    {                             
        checkElems(pixelTypes[p]);
    }                            
}, 10);

// Add Document finished callback.
$(document).ready(function ()
{         
    // Document is loaded, so stop trying to find new pixels
    clearInterval(timer); 
});       

In Firefox, it works great, but in IE6, I get a "Object Expected" error on the $(document).ready line.

I can't figure out what would cause IE6 to not recognize it, jquery is fully loaded by this point.

Is this a known issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just a few pointers for anyone that's interested:

$(document).ready(function() {...}); and $(function() {...}); means exactly the same thing. The latter is a shorthand for the former.

If you develop for a large site, using multiple Javascript libraries, or you develop plugins meant to be compatible with other peoples work, you can not trust the dollar sign ($) to be associated with the jQuery object. Use the following notation to be on the safe side:

(function($) { [your code here] })(jQuery);

This passes jQuery into a self-executing function, and associates $ with the jQuery object inside this function. Then it does not matter what the $ represents outside of your function.

To get back to your question, have you checked whether the timer variable is assigned when you get the error? I believe the browser will see the $(document).ready(function() {...}); all as one line, so if you have some kind of debugger that tells you that's the offending line, it might be the timer variable...

Last thing: In Javascript, it is not correct to place open curly braces on a new line. This can cause really bad errors due to Javascripts semicolon-insertion. For further info, read Douglas Crockford's Javascript: The good parts:

http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742/ref=sr_1_1?ie=UTF8&s=books&qid=1267108736&sr=1-1

Anyway, really hope I didn't upset anyone. Hope you solve the problem!

EDIT: I'm not sure if this is what robertz meant by fully qualified, but as far as I know, when a URL is fully qualified it means no parts are missing, ie. it's an absolute URL starting with http:// or https:// (or some other protocol). Please correct me if I'm wrong!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...