具有负值的substr()在IE中不起作用 [英] substr() with negative value not working in IE

查看:283
本文介绍了具有负值的substr()在IE中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我已经改变了标题,因为这个问题与IE无关.gmage.load()触发 - 我的substr()无法正常工作(见接受的答案)。






有很多关于确保在分配img.src之前定义onload处理程序的帖子,以保证onload处理程序在如果首先从缓存中加载图像。



这似乎不是我的代码中的问题,因为这正是我所做的。



请注意,此脚本适用于所有其他浏览器,但IE 8及更低版本不会触发onload内联函数。

  var i = lastSlideIndex || 1; 

while(imagesQueued< MAX_IMAGES){
var preloadImage = new Image();
preloadImage.arrayIndex = i;
preloadImage.onload = function(eventObj){
debug('Image'+ this.src +'loaded。');
slideshowImages [this.arrayIndex] = this;
if(this.arrayIndex> lastImageIndex)lastImageIndex = this.arrayIndex;
triggerSlideshow(this.arrayIndex);
}

//添加前导零
var leadingZeros =0000000.substr( - (String(MAX_IMAGES).length));
imageNumber = leadingZeros.substr(String(i).length)+ i;

debug('preloading Image#'+ imageNumber);
preloadImage.src = fullImagePrefix + imageNumber +。 + IMAGES_TLA;

if(++ i> MAX_IMAGES)i = 1;
imagesQueued ++;
}

任何其他建议都将深表感谢!

解决方案

更新:正如评论者指出的那样(我现在不能证明他们),我删除了第一个建议。



还有几点需要注意:



onload 如果从缓存加载图像,则不会触发事件。尝试清除缓存并重试。



另一个问题是IE在 substr 中不喜欢负数。改为使用 slice

 0000000.slice( - (String (MAX_IMAGES)。长度)); 


EDIT:I've changed the title, because the issue had nothing to do with IE image.load() firing - my substr() wasn't working (see accepted answer).


There's a ton of posts about making sure that you define your onload handler prior to assigning img.src in order to guarantee that the onload handler is in place in case the image is loaded from cache first.

This does not appear to the be issue in my code, since that's precisely what I have done.

Note that this script works across all other browsers, but IE 8 and lower does not trigger the onload inline function.

var i = lastSlideIndex || 1;

while(imagesQueued < MAX_IMAGES){
    var preloadImage = new Image();
    preloadImage.arrayIndex = i;
    preloadImage.onload = function(eventObj){
        debug('Image ' + this.src + ' loaded.');
        slideshowImages[this.arrayIndex] = this;
        if (this.arrayIndex > lastImageIndex) lastImageIndex = this.arrayIndex;
        triggerSlideshow(this.arrayIndex);
    }

    // add leading zeros
    var leadingZeros = "0000000".substr(-(String(MAX_IMAGES).length));
    imageNumber = leadingZeros.substr(String(i).length) + i;

    debug('preloading Image #' + imageNumber);
    preloadImage.src = fullImagePrefix + imageNumber + "." + IMAGES_TLA;

    if (++i > MAX_IMAGES) i = 1;
    imagesQueued++;
}

Any other suggestions would be deeply appreciated!

解决方案

Update: As commenters have pointed out (and I can't prove them otherwise for now), I removed the first suggestion.

Couple of more things to note:

onload event will not fire if the image is being loaded from cache. Try clearing your cache and retry.

Another problem is that IE doesn't like negative in substr. Use slice instead:

"0000000".slice(-(String(MAX_IMAGES).length));

这篇关于具有负值的substr()在IE中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆