此代码应检查服务器上是否存在多个图像;它工作正常...但是仅用于第一张图片;为什么不按FOR循环的要求重复检查10次?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
<script>
var img;
var URL_BASE;
var URL_DATE;
var d;
function fnTest() {
d=new Date(localStorage.getItem("date"));
console.log("D="+d);
for (i=1; i<=10; i++); {
URL_DATE = d.getFullYear() +
('0' + (d.getMonth()+1)).slice(-2) +
('0' + d.getDate()).slice(-2)+
('0' + d.getHours()).slice(-2) +
('0' + d.getMinutes()).slice(-2)
console.log("URL_DATE="+URL_DATE);
var URL_IMAGENAME = URL_DATE + ".jpg";
img.src = URL_IMAGENAME;
var d = new Date( d.getTime() - 1000 * 60 );
console.log("NEW="+d);
}
}
function initVars() {
console.log("initvars");
img = new Image();
img.onload = ImageOk;
img.onerror = MissingImage;
URL_BASE = "http://www.hayabusa2.jaxa.jp/en/galleries/onc/nav20181002/";
URL_DATE;
d = new Date("2018/10/04 23:59");
console.log(localStorage.setItem("date",d));
//document.addEventListener('btnTest', fnTest);
console.log("OK:"+d);
}
document.addEventListener('DOMContentLoaded', initVars);
function MissingImage() {
console.log("bad");
}
function ImageOk () {
console.log("good");
}
</script>
</head>
<body>
<button name="btnTest" id = "btnTest" onclick="fnTest()">Test</button>
</body>
</html>
我通过将功能链接到图像对象上的“错误”事件来执行检查,这是跨站点脚本安全限制的一种解决方法。 无论图像是否存在,该代码都能很好地工作,但是在第一个图像之后停止。