If all images on page return an error redirect?
How can I make so if all the images on the page are 开发者_Go百科broken/don't exist javascript will redirect to another page?
I want to use this in an image gallery type script.
Give this script a try:
var imgCount = 0, imgErrorCount = 0;
function onImgError(increment) {
if(increment) {
imgErrorCount++;
}
if(imgCount > 0 && imgCount == imgErrorCount) {
location = '/url-to-redirect';
}
}
$(function(){
imgCount = $('img').length;
onImgError(false);
});
And for every img element:
<img src="..." onerror="onImgError(true);" />
精彩评论