检查图像是否存在而不加载它 [英] Check if image exists without loading it

查看:68
本文介绍了检查图像是否存在而不加载它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在悬停功能中使用以下脚本:

I am currently using following script in a hover-functionality:

function UrlExists(url) {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}

它会逐个加载每张图片,导致整个网站变慢(甚至崩溃)。

It loads every image each after the other, causing to slow down the entire website (or even crashing).

有没有办法检查图片是否存在,但阻止加载 (完全)使用javascript?

Is there a way to check if an image exists, though prevent loading it (fully) using javascript?

非常感谢!

推荐答案

JavaScript(以及jQuery)是客户端的,图像在加载之前驻留在服务器端,没有办法在不使用Ajax或服务器端脚本的情况下检查图像是否存在以确保图像存在。

Since JavaScript (and therefore jQuery) is client-side and the image resides server-side before loading there is no way to check to see if the image exists without using Ajax or your server-side scripting to make sure the image exists.

这篇关于检查图像是否存在而不加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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