& # 39;负载# 39;事件在img 302重定向中被调用?如何测试这个
Does 'load' event get called on img 302 redirects? How to test for this?
我认为浏览器在加载图像(只要不是500或其他错误代码)获得的第一个HTTP响应上调用"load"事件
例如- Img url返回302临时移动
- 浏览器在img标签上错误地调用"load"事件
- 第二次HTTP请求成功,返回200
- 图像现在实际加载
有人知道测试这个的方法吗?
我错了,我很困惑。
加载事件在javascript上下文中正确发生。
问题是浏览器如何处理302d图像,它的缓存和CSS过渡。
例如,如果你在图片加载时调用不透明CSS过渡,每个浏览器将以不同的方式处理它。
Chrome在缓存的302图像上工作转换Firefox会在所有302d图像上进行转换,而不考虑缓存无论HTTP状态或缓存如何,Safari都能完美地处理转换。
请看下面的演示:http://jsfiddle.net/2TCy4/33/
jsfield演示代码:
HTML:<figure></figure>
<div>
<button id="withCache">Load with image cache</button><br/>
<button id="withoutCache">Load with no image cache</button>
</div>
<pre>
Loads will alternate between loading a 200 success and a 302 redirect of the same image
When imagesLoaded determines an img isLoaded,
a class is added and the opacity transition should begin.
On Chrome, After the first "cycle" with image cache,
cached image buggyness becomes apparent as CSS transitions on the 302 redirect are jittered.
On Firefox, in both cache situations, 302 redirects are still bugged.
This implies Firefox uses the cached image from the 2nd HTTP request.
In Safari, transitions work perfectly regardless of cache or HTTP status.
</pre>
Javascript: var good200 = $('<img src="https://24.media.tumblr.com/tumblr_lsoemgev4A1qh6npeo1_1280.jpg">');
var bad302 = $('<img src="http://www.tumblr.com/photo/1280/lesfascinations/11273750776/1/tumblr_lsoemgev4A1qh6npe">');
var now = Date.now();
var caption = $("<figcaption></figcaption>");
var is302 = false;
var withCache = false;
// 1. Click a button
$('button').click(function (e) {
withCache = $(this).is("#withCache");
now = Date.now();
$('figure').empty();
setTimeout(insertImage, 0);
});
// 2. Insert alternating 302/200 img
var insertImage = function () {
$('figure').html(is302 ? get302img() : get200img());
setTimeout(runImagesLoaded, 0);
}
// 3. Run imagesLoaded script
var runImagesLoaded = function () {
$('figure').imagesLoaded().progress(function (instance, item) {
if (item.isLoaded) {
$(item.img)
.closest('figure')
.append(
caption.clone().text('imagesLoaded: ' + (Date.now() - now) + "ms")).append(is302 ? '302 redirect' : '200 success');
setTimeout(addLoadedClass($(item.img)), 0);
//alternate 302 and 200 img requests
is302 = !is302;
}
});
}
// 4. Add loaded class to img when it is loaded
var addLoadedClass = function (el) {
el.addClass("loaded");
}
var get302img = function () {
var img = bad302.clone();
if (!withCache) appendRandomParam(img);
return img;
}
var get200img = function () {
var img = good200.clone();
if (!withCache) appendRandomParam(img);
return img;
}
var appendRandomParam = function (img) {
var timestamp = Date.now();
img.attr("src", img.attr("src") + "?" + timestamp);
}
CSS: img {
width: 100px;
transition: opacity 2s;
-moz-transition: opacity 2s;
-webkit-transition: opacity 2s;
-o-transition: opacity 2s;
opacity: 0;
-moz-opacity: 0;
-webkit-opacity: 0;
-o-opacity: 0;
}
img.loaded {
opacity: 1;
-moz-opacity: 1;
-webkit-opacity: 1;
-o-opacity: 1;
}
figure {
float: left;
}
figcaption {
text-align: center
}
pre{
}
}
一个潜在的演示是延迟添加css加载类100毫秒或更多,如下所示:http://jsfiddle.net/2TCy4/34/
相关文章:
- 模糊事件的Javascript测试
- 我的单元测试选项是什么
- 测试索引值是否等于某个数字的倍数
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 测试数组中每个项的内容
- 测试Angular Service解决错误回调中的promise
- 使用Jest测试React Native应用程序
- 为函数代码编写测试
- Javascript Mocha测试-测试函数表达式中的函数表达式
- 使用超级测试测试 koa 路由时,内容类型标头始终相同
- 使用磁带/ES6 单元测试测试 AMD 模块
- 用茉莉花测试测试ng变化
- 单元测试 - 测试文件计数和文件夹大小
- 节点.js单元测试:测试fs模块抛出错误时的行为
- 如何使用因果报应运行基本测试(测试)
- 使用Jasmine进行单元测试:测试没有看到beforeEach()中的代码's spyOn()
- 用Jasmine单元测试测试Angular模块
- 单元测试:测试内部承诺
- 使用Visual Studio和Team City,用单元AND集成测试测试javascript的正确方法是什么
- 超级测试测试快速中间件