views:

13833

answers:

16

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?

A: 

I don't think that you should really be polling for elements the way you are.

The document ready event calls as soon as the browser has loaded enough for you to be able to manipulate the page, so you should just do your DOM processing in the $(document).ready() block.

Ben Alpert
I have to process before the DOM is fully loaded, thats the point of this script, to measure 3rd party element load times.
FlySwat
+1  A: 

Are you sure that jQuery is loaded? Try debugging with alerts like:

alert(typeof $);

You could also try a different syntax:

$(function() {
     clearInterval(timer); 
});


Ok, so from your comment, the above doesn't help. The "object expected" error seems to occur with a syntax error in my experience. Is that the exact code you've got? If not, could you post it?

nickf
typeof($) is a function, first thing I checked. Both syntaxes fail.
FlySwat
+1  A: 

You could try the old skool way of checking whether the document is "ready"... Place the script just before the closing </body> tag - I believe it has the same effect as jQuery's 'ready' event - actually, it's probably quicker doing it this way.


In my experience the "Object expected" error in IE6 shows up because of a syntax error - it's worth putting the script though JSlint, if you haven't already...

J-P
A: 

I ran into this problem on my machine, as was able to find a quick fix. Here's what I did:

1.Debugged my javascript with nickf's suggestion "alert(typeof $)" and got the "undefined" alert message

2.I then fully qualified my jQuery script resources.

3.Reload my page and received the "function" alert message

BTW, I am using IIS 5.1 on XP. My website is configured to use "Wildcard mapping" to take advatage of the asp.net mvc framework. I think that this configuration caused the broken links.

For more information on how to setup MVC on old versions of IIS, check out Phil Haack's post: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx

robertz
A: 

Can anyone explain me what does it mean by fully qualifying jQuery script resources..?

A: 

I also have the same problem and am not able to clear it yet. I am using Visual Studio 2008 and the project is on 2.0 framework.

I have checked whether the jscript is file is refrenced correctly or not. It doesnt help either.
Then i ran the code in mozilla and check the error console..it says that "$ is not defined".

Help me out guys.

Vipul Raj Mohan
A: 

Can anyone explain me what does it mean by fully qualifying jQuery script resources..?

viperasi
+3  A: 

I've had this same issue in the past too. It was a sporadic issue and was horrible to and reproduce.

The solution that I found was to replace $(document).ready(function() {...}); with jQuery(function() {...}) and it worked like a charm!

Moving $(document).ready(function() {...}); to the bottom didn't work for my use case.

The comments in this post are incredibly helpful (Where I first read about doing it this way)

Colin Harrington
+1  A: 

I'm having the same problem in IE6 when including the code into my page by AJAX (own importing function, not jQuery). Both $(document).ready(function() {...}); and jQuery(function() {...}); works fine in FF. It also works fine in IE when not inserted by AJAX.

The AJAX importing executes the imported html code's javascripts before actually inserting the html, where the elements to be manipulated exists. I assume IE actually run the script within $(document).ready()/jQuery() before the html code is inserted, and that's what's causing the problem.

I'm not very experienced with jQuery, but if any of you know of a 'correct' solution for this, I appriciate it. Else I just have to modify the importing script with a setting to be able to insert the html before executing the scripts...

lars-m
Is there any way to ajax the html in first and then the javascript? Break up into two ajax calls?
Nosredna
Thanks for replying. Yes, as I suggested I guess I just have to add a setting/parameter to the importing script to allow this. Perhaps to allow "normal" behavior, scripts must be executed and html inserted in the order it's defined in the code to import, except for $(document).ready(), which should be run when everything else is done. But then, if the code imported have a lot of script tags within the html body, I'm not sure how this will affect performance; breaking up the code, inserting one part at the time.Still curious how $(document).ready is able to make this work in FF...
lars-m
A: 

$(document).ready() tells you when the dom is ready, but not all assets are necessarily done coming in.

If you want to make sure all the assets are actually done loading, use $(window).load() instead. The most common use for this is to make sure that images are done loading, but it may work for your script problem as well.

Nosredna
+1  A: 

If it is in a script element which is within your body element, (i.e.) ..

The cause can be the attributes you pass with the script-tag. If it is:

<script type="text/javascript">...</script>

IE6 can give an error. You should use

<script language="javascript">...</script> 

Then the error goes away.

Martin Sturm
+3  A: 

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&amp;s=books&amp;qid=1267108736&amp;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!

Adrian Schmidt
+1  A: 

If anyone have the same problem you should see if when you call your javascripts you have type="application/javascript", I eliminate it and it was corrected, I think it's some problem with IE and the type Thing

yuukan
Thanks a lot. Nothing worked for me. This was a like a miracle. I tried debugging jQuery.js to all the plugins. Finally. Thanks yOU.
SKR
A: 

Put new version of the jquery .from that You got solution. From my case that is the problem

ramesh
A: 

I had similar issue, but I resolved by putting latest Jquery version.

Try your luck as well !

Niket Shah
A: 

Make sure your script type is text/javascript

<script type='text/javascript'
unbnd