简体   繁体   English

在 IE8 中不起作用 window.onload

[英]Does not work window.onload in IE8

I have a pretty simple image switcher, which I use for manual images switcher and for carousel.我有一个非常简单的图像切换器,用于手动图像切换器和轮播。 In IE 8 it works strange.在 IE 8 中,它的工作方式很奇怪。 In carousel scenario image switches just once, thereafter it's dies.在轮播场景中,图像只切换一次,然后就死了。 But (!) when I implemented Firebug Lite and try to trace - it's works well, only with firebug on ... I tried some tricks, I found, but it's all to no purpose.但是(!)当我实现 Firebug Lite 并尝试跟踪时 -它运行良好,只有在 firebug 上运行......我尝试了一些技巧,我发现,但这一切都没有任何意义。 I have no idea what caused this kind of behavior.我不知道是什么导致了这种行为。 How to fix it?如何解决?

js js

function toSlide(wrapper, options){
    var active = $('#' + wrapper + ' div.active');
    var slide;
    var direction;

    if (active.length === 0){
        active = $('#' + wrapper + ' div:last');
    }

    if (options === null) {
        options = {};
        options.reverse = false;
        options.animate = false;
    } else {
        options.reverse = options.reverse === null ? false : options.reverse;
        options.animate = options.animate === null ? false : options.animate;
    }

    direction = options.reverse === true ? active.prev() : active.next();

    slide = direction.length ? direction : $('#' + wrapper + ' div:first');

    if (options.animate === true){
        active.addClass('last-active');

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

function startSlideShow() {
    setInterval(function(){ toSlide('slideshow', {'animate': true}); }, 5000);
};

window.onload = function() {  
    if (document.location.pathname == '/'){startSlideShow();};
};

html in head html在头

<!--[if IE 8 ]>  
<link rel="stylesheet" href="{{ MEDIA_URL }}css/ie8.css" type="text/css" media="screen, projection" />  <html lang="en" class="no-js ie8"> 
<script defer src="ie_onload.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="{{ MEDIA_URL }}js/jquery-1.6.2.js"%3E%3C/script%3E'))</script> 
<script type="text/javascript" src="http://fbug.googlecode.com/svn/lite/branches/firebug1.3/content/firebug-lite-dev.js"></script>
<![endif]-->

in bottom of html在 html 的底部

<!-- Grab Google CDN's jQuery. fall back to local if necessary -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<!-- <script>!window.jQuery && document.write(unescape('%3Cscript src="{{ MEDIA_URL }}js/jquery-1.4.2.js"%3E%3C/script%3E'))</script> -->

<!-- scripts concatenated and minified via ant build script-->
<script src="{{ MEDIA_URL }}js/plugins.js"></script>
<script src="{{ MEDIA_URL }}js/script.js"></script>
<!-- end concatenated and minified scripts-->

Accessing console in FF without Firebug open or in IE will result in an error that will block the rest of your JS from executing.在没有打开 Firebug 的情况下或在 IE 中访问 FF 中的console将导致错误,该错误将阻止您的 JS 的 rest 执行。 If you want to leave calls to console in, you should implement them like one of the following:如果您想保留对console的调用,您应该像以下之一那样实现它们:

try { console.log('blah'); } catch(e) {}
// or
if (typeof console != 'undefined') console.log('blah');

Or use a custom built logging function that implements something like the above.或者使用实现类似上述内容的自定义构建日志记录 function。

The cause was non-functional window.onload in IE8.原因是 IE8 中的 window.onload 无法正常工作。 I did right trick but made stupid mistake.我做了正确的把戏,但犯了愚蠢的错误。 So, I fixed it:所以,我修复了它:

was曾是

<!--[if IE 8 ]>
    <script defer src="ie_onload.js"></script>
<![endif]-->

is now就是现在

<!--[if IE 8 ]>  
    <script defer src="{{ MEDIA_URL }}js/ie_onload.js"></script>
<![endif]-->

in ie_onload.js在 ie_onload.js

document.body.onload = function() {
    imageViewer();
    // and other functions
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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