1

I am seeing a number of these errors in my JavaScript error logs:

Object expected

TypeError: Property '$' of object [object Object] is not a function

Unfortunately, I cannot replicate the error on any of these browsers when I try it myself. The line that I have highlighted is the one that is causing the error.

I have a read a bit about "No Conflict" mode and that may be a problem here, but I can't see what the issue would be by looking at the code that is below.

I am using jQuery 1.7.2 and it is served up from my server, rather than a CDN:

<script type="text/javascript" src="/scripts/jquery/jquery-1.7.2.min.js"></script>

My code:

$(function() {

    $('.imgCell').live("mouseenter", function() {
        if($(this).find('a img').length > 1) { // this line throws the error
            $(this).find('a img:eq(0)').hide();
        }   
    });

});

It does not appear to be affecting one specific browser either, as the following are affected: Chrome 26, Chromium 25, Firefox 10, Firefox 14, Firefox 16, Firefox 20, IE 10, IE 8, IE 9, Mobile Safari 6

crmpicco
  • 16,605
  • 26
  • 134
  • 210

2 Answers2

1

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

$(document).on("mouseenter",'.imgCell', function() {
        //do something
    });

if you're using CDN, then you need to write fallback for it

<script type="text/javascript" src="//ajax.microsoft.com/ajax/jquery/jquery-1.9.2.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') { // load your JS file if CDN failed 
    document.write(unescape("%3Cscript src='/js/jquery-1.9.2.min.js' type='text/javascript'%3E%3C/script%3E"));   
}
</script>
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
  • No, you are not using delegation here! Should be for complete equivalent to live: $(document).on('mouseenter','.imgCell',function(){...}); BTW, this has nothing to do with OP's question – A. Wolff May 02 '13 at 10:58
  • document is not a tag element but an object: $(document) not $('document') – A. Wolff May 02 '13 at 11:01
  • Thanks for pointing out that `live` is deprecated, however is this why the error is occurring? Also, would it not error on the `live` line, rather than the line following it? – crmpicco May 02 '13 at 11:18
  • @Ravi Looks like there's a syntax error in your code on the first line (missing comma). – crmpicco May 02 '13 at 12:08
  • @Ravi I'll certainly give this a try, but as I don't seem to see the problem myself and can't confirm it's resolved i shall wait until this clears up in my JS error logs to mark as accepted. Thanks. – crmpicco May 03 '13 at 08:37
0

use 'jQuery' instead of $ sign for example

jQuery(this).find('a img').length > 1
dev
  • 11
  • Why would the line that errors cause the error, but the reference to `$` in the line not error? – crmpicco May 02 '13 at 11:15
  • I am not getting you, can you please explain your comment ? I had the same issue some time before and applied the mentioned solution which resolved the error. let me know if it works for you. – dev May 02 '13 at 15:00
  • Well, from your answer it would suggest that the reference to `$` is a problem. But there is a reference to `$` in the line above, but it does not error. Why would that be? BTW, I did not downvote you. – crmpicco May 03 '13 at 08:35