【发布时间】:2012-06-06 07:46:33
【问题描述】:
在http://perishablepress.com/3-ways-preload-images-css-javascript-ajax/找到这种使用 ajax 预加载内容的技术
window.onload = function() {
setTimeout(function() {
// XHR to request a JS and a CSS
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.tld/preload.js');
xhr.send('');
xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.tld/preload.css');
xhr.send('');
// preload image
new Image().src = "http://domain.tld/preload.png";
}, 1000);
};
我注意到此图像的“ajax”预加载根本不是真正的 ajax。它和我多年来一直使用的一样,只是将 url 设置在新图像对象的源中,然后让浏览器将其加载到缓存中。
现在假设有一个应用程序,如果它占用了一定的时间,我需要实际取消图像的预加载。仅将图像设置为 src 确实没有什么好的方法,这与停止加载实际 xhr 请求的 xhr.abort() 方法不同。
是否有任何原因导致执行以下操作不能很好地预加载图像并允许取消预加载请求?
function preload(url, timeout){
this.canceltimeout = function(){
clearTimeout(this.timeout);
this.loaded = true;
return false;
}
this.abort = function(){
this.xhr.abort();
this.aborted = true;
}
//creates a closure to bind the functions to the right execution scope
this.$_bind = function(method){
var obj = this;
return function (e){ obj[method](e);};
}
//set a default of 10 second timeout
if(timeout == null){
timeout = 10000;
}
this.aborted = false;
this.loaded = false;
this.xhr = new XMLHttpRequest();
this.xhr.onreadystatechange = this.$_bind('canceltimeout');
this.xhr.open('GET', url);
this.xhr.send('');
this.timeout = setTimeout(this.$_bind('abort'), timeout);
}
var llama = new preload('/image.gif');
show_image();
function show_image(){
if(llama.loaded){
var l = new Image();
l.src = '/image.gif';
application.appendChild(l);
}else if(llama.aborted){
var l = document.createElement('p');
l.innerHTML = 'image.gif got cancelled';
application.appendChild(l);
}else{
setTimeout(show_image, 10);
}
return false;
}
【问题讨论】:
-
使用 jquery man。 api.jquery.com/jQuery.ajax
-
可以使用 jquery,但这与使用 xhr 对象请求图像是否足以将其加载到缓存中没有任何关系。 Jquery 相当庞大,当您只需要它的一小部分时,它真的无济于事。
-
我看到我发布的 show_image 函数有点乱。 application 是来自其他一些 javascript 的全局变量,我将这段代码添加到其中以进行尝试。它似乎有效,但我没有很多不同的浏览器或关于它们的缓存的知识可以从中提取,看看这是否可以跨浏览器的方式工作。
-
它可能不起作用的一个原因是由于跨域策略,此外它看起来不错。
-
不用担心跨域政策,因为网站上所有考虑过这一问题的图片都是直接从我们的服务器提供的,但即使没有,您也可以通过一点服务器端脚本魔法轻松解决这个问题如果调用的是服务器端脚本而不是图像,则检索图像并将其返回。
标签: javascript ajax image preload