简体   繁体   English

为什么我的图片加载在Firefox和Internet Explorer中无法启动?

[英]Why is my image onload not firing in Firefox and Internet Explorer?

I'm trying to detect when a few images are done loading using the solution found here 我正在尝试使用此处找到的解决方案检测何时加载了一些图像

The solution works wonderfully in Chrome and Safari but fails (without error) in both Firefox and IE. 该解决方案在Chrome和Safari中效果很好,但在Firefox和IE中均失败(没有错误)。

The preloading function is the following: 预加载功能如下:

var preloadPictures = function(pictureUrls, callback) {
    var i,
        j,
        loaded = 0;

    for (i = 0, j = pictureUrls.length; i < j; i++) {

        (function (img, src) {
            img.onload = function () {
                if (++loaded == pictureUrls.length && callback) {
                    callback();
                }
            };

            img.src = src;
        } (new Image(), pictureUrls[i]));
    }
}; 

And I use it by creating an array from the background images of a few divs. 我通过从几个div的背景图像创建一个数组来使用它。 The function loadSlides is called when the document is ready. 准备好文档后,将调用loadSlides函数。 Once all pictures are loaded my slider's controls are told to fade in. 加载完所有图片后,告诉我的滑块控件淡入。

function loadSlides() {
    if( jQuery('#frontpage-slider')[0] ) {
        var pictures = [];

        jQuery('#frontpage-slider .slide').each(function() {
            var bg = $(this).css('background-image');
            bg = bg.replace('url(','').replace(')','');
            pictures.push( bg );            
        });

        preloadPictures( pictures, function() {
            jQuery('.slide-controls').css('visibility','visible').hide().fadeIn('slow');
        } );
    }
}

If I alert the pictures variable I get an array with the following values so I don't think my problem has anything to do with the values. 如果我警告pictures变量,则会得到一个包含以下值的数组,因此我认为我的问题与这些值无关。

 http://foo.bar/content/user_files/2014/08/test-2.png

 http://foo.bar/content/user_files/2014/08/test-1.png

I tried a few other solutions in the thread linked above but none seem to have worked. 在上面链接的线程中尝试了其他一些解决方案但似乎没有一个起作用。 I tried the jQuery solution, setting img.src = ''; 我尝试了jQuery解决方案,设置img.src = ''; prior to setting the actual source but nothing happens. 在设置实际来源之前,但没有任何反应。

Any help is appreciated. 任何帮助表示赞赏。

Edit: I created the following jsfiddle: http://jsfiddle.net/yuaond6b/2/ 编辑:我创建了以下jsfiddle: http : //jsfiddle.net/yuaond6b/2/

I get the same problem with that script as it works in Chrome but does nothing in Firefox. 我在该脚本中遇到了同样的问题,因为它可以在Chrome中使用,但在Firefox中什么也不做。 I had to modify the script as for some reason jsfiddle didn't like my variable function but the result is the same. 由于某些原因,我不得不修改脚本,因为jsfiddle不喜欢我的变量函数,但结果是相同的。

Edit 2: If I add an "onerror" function to my img it gets triggered. 编辑2:如果我在img中添加“ onerror”功能,则会被触发。 Unfortunately I've tried to extract the message from these errors and can't seem to figure out exactly how it works. 不幸的是,我试图从这些错误中提取消息,并且似乎无法确切地了解它是如何工作的。

Firefox is breaking because the line of code: Firefox之所以破产,是因为代码行:

var bg = jQuery(this).css('background-image');

is returning the URL with quotes: 返回带有引号的网址:

url("https://i.imgur.com/fTb97EO.jpg")

whereas in Chrome it returns it without quotes: 而在Chrome中,它不带引号返回:

url(https://i.imgur.com/fTb97EO.jpg)

You then strip the url( and the ) which works fine in Chrome, but it means in Firefox the string bg has additional quotes around it. 然后,您去除url(和),这在Chrome中可以正常工作,但这意味着在Firefox中,字符串bg周围带有附加引号。 This means when you set the src attribute, Firefox converts those extra quotes to %22 and then doesn't recognise the URL and attempts to load a local path. 这意味着当您设置src属性时,Firefox会将那些多余的引号转换为%22,然后无法识别URL并尝试加载本地路径。 Here is what Firefox tries to load when run in jsfiddle: 这是Firefox在jsfiddle中运行时尝试加载的内容:

http://fiddle.jshell.net/yuaond6b/4/show/%22https://i.imgur.com/S1OPVB6.jpg%22

The solution is to strip the " in Firefox, but not in Chrome, using a regexp: 解决方案是使用regexp在Firefox中删除“,而不在Chrome中:

bg = bg.replace(/url\(["]*/,'').replace(/["]*\)/,'');

as shown here: 如下所示:

http://jsfiddle.net/yuaond6b/6/

This works OK in both Chrome and Firefox! 在Chrome和Firefox中都可以正常运行!

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

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