简体   繁体   中英

JQuery rotator broken in IE8 & 9 only

I'm running the following code to create a JQuery image fader:

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');   

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});

This works fine in all browsers except IE 8 & 9. I'm seeing the following errors in IE8:

Object Expected

Object doesn't support this property or method

These errors are for the 1st character of this line:

$(function() {
    setInterval( "slideSwitch()", 5000 );
});

The site is running in the HubSpot CMS. I'm calling both JQuery and the JS above in the section of the home page:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://XXXXXXXX.com/Portals/XXXXXXX/js/slider.js"></script>

Maybe IE is blocking the file from ajax.googleapis.com - in IE9, when in your page, click F12 go to Network tab click "Start capturing" and reload the page.

You should see such line:

What you get as the Result value?

I tested this, got the error using the version of jQuery you had, got the error but then replaced the code as:

$(function() {
    setInterval(function(){slideSwitch()}, 5000);
});

This then did not get the error for me: http://jsfiddle.net/RFTxE/1/

User error. (This was my question)

I couldn't provide access to the site itself b/c it was behind a password, but I had a friend look into it for me and:

There was an unclosed conditional comment loading IE7 CSS right before the script call, and another (properly closed) conditional comment right after the script call. The greater IEs thought that the entire bit was meant for 7 only. The other browsers knew better.

All this would have been insanely simple for me to catch in a code validator, but if you've ever tried to validate a HubSpot site you'd understand.

Thanks all so much for your help! I really really appreciate it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM