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.

Read More

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

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

Please help me to figure it out.

Thank you.

Related posts

Leave a Reply

3 comments

  1. 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.

  2. 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){
    
    }
    
  3. 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();
        }
    });