具有负值的 substr() 在 IE 中不起作用
编辑:我更改了标题,因为该问题与 IE image.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);
}
// 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++;
}
任何其他建议将不胜感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在这个问题被重新命名为关于 IE 8 中的
substr()
,下面是负索引问题的快速修复。粘贴 中的以下代码Mozilla 开发者网络:此代码检测 substr 的损坏实现并将其替换为兼容的实现。
Now that this question is retitled to be about
substr()
in IE 8, here's the quick fix to the negative index problem. Paste the following code from Mozilla Developer Network:This code detects the broken implementation of substr and replaces it with a compliant one.
更新:正如评论者所指出的(我现在无法证明他们),我删除了第一个建议。
还有几点需要注意:
如果从缓存加载图像,则不会触发
onload
事件。尝试清除缓存并重试。另一个问题是 IE 不喜欢
substr
中的负数。使用slice
代替: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
. Useslice
instead:处理此问题的两种方法:
向图像 URL 添加随机数参数。
在不同的事件循环中设置“src”属性。
通过在每次获取图像时使用随机数参数,您可以绕过缓存。现在,这可能不是一个好主意,因此第二个选项通过确保在单独的事件循环中设置“src”属性来解决该问题。然后将触发“负载”。
这里是一个示例。该代码在某些图像上使用随机数,但在超时处理程序中为所有图像设置“src”。如您所见,它们全部加载(变成红色)。
我不知道为什么 IE 在图像位于缓存中时不触发“load”处理程序,但当“src”设置在与图像对象(否则)初始化的事件循环不同的事件循环中时,它会触发“load”处理程序。
编辑 - 这里是同一个小提琴,但经过修改以跳过超时。在 IE8 中,您应该注意到偶数图像通常不会调用“加载”处理程序。
Two ways of dealing with this:
Add a nonce parameter to your image URLs.
Set the "src" property in a different event loop.
By using a nonce parameter each time you fetch an image, you bypass the cache. Now, that's probably not such a great idea, so the second option gets around the problem by making sure that the "src" property is set in a separate event loop. The "load" will trigger then.
Here is an example. The code uses nonces on some images but sets the "src" for all of them in a timeout handler. As you can see, they all load (turn red).
I don't know why IE doesn't fire the "load" handler when the image is in cache, but it does when the "src" is set in a different event loop from where the image object is (otherwise) initialized.
edit — Here is that same fiddle, but modified to skip the timeout. In IE8, you should notice that the even-numbered images often don't get a call made to the "load" handlers.