Jone
Jone

Reputation: 53

Object doesn't support this property or method

As titled, I'm getting this error on my site. I have checked through the IE8 developer debugging tool, and I have got the following code that caused the error.

<!-- slider js code start -->
<script type="text/javascript">
$().ready(function() {
    if(eval(document.getElementById('coda-slider-1')))
    {
        $('#coda-slider-1').codaSlider();
        //jQuery.noConflict(); var $j = jQuery;
    }
}); 

I have included the screenshoot from Chrome debugging tool.

http://img857.imageshack.us/i/javaerror.jpg

http://img204.imageshack.us/i/javaerror2.jpg

Please help me to figure it out.

Thank you.

Upvotes: 3

Views: 21692

Answers (3)

Erick Petrucelli
Erick Petrucelli

Reputation: 14892

Problems:

  • $().ready isn't recommended. Use $(document).ready(function or simple $(function.

  • Why using document.getElementById if jQuery already lookup elements using selectors in a crossbrowser way? Just do $("#some").length to see if it exists.

  • Also in your case I think is good to ensure that the codaSlider() method is loaded before calling.

Correted code:

$(function() {
    if ($("#coda-slider-1").length > 0 && $("#coda-slider-1").codaSlider) {
        $('#coda-slider-1').codaSlider();
    }
});

Upvotes: 0

Ender
Ender

Reputation: 15221

Try this instead:

$(function() {
    if($('#coda-slider-1').size())
    {
        $('#coda-slider-1').codaSlider();
        //jQuery.noConflict(); var $j = jQuery;
    }
}); 

Your original code says "select nothing with jQuery, and apply this ready handler to it." The correct long-hand syntax would be:

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

Also note that I have removed eval because it should never, ever be used, unless it can't possibly be avoided.

UPDATE

Looking at your error screenshots, it appears that jQuery is not defined (at least not with the $ alias. Have you included the script on your page? If so, are you calling jQuery.noConflict() before your ready handler is bound?

Try putting this script tag above both the code you posted, and the script tag for the coda slider:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>

UPDATE 2

As pointed out by kingjiv in the comments below, I was mistaken, and $().ready will work (though it is not recommended). I believe my first update, noting that jQuery appears not to be defined, is the actual issue here.

Upvotes: 4

James Montagne
James Montagne

Reputation: 78650

I'm guessing you're trying to check for the existence of coda-slider-1?

No need to use eval, and if you're using jquery, may as well select the element with jquery:

if($("#coda-slider-1").length>0){

}

Upvotes: 0

Related Questions