我正在尝试在页面中写入有效images
(存在的图像)的 url html
。这是输入图像的列表,添加在变量中urls
:网址是现有和不存在的谷歌产品图标的网址,例如
var urls = [
"https://www.google.com/images/icons/product/chart-32.png",
"https://www.google.com/images/icons/product/docs-32.png",
"https://www.google.com/images/icons/product/drive-32.png",
"https://www.google.com/images/icons/product/googlemail-32.png",
"https://www.google.com/images/icons/product/dropbox-32.png",
"https://www.google.com/images/icons/product/reader-32.png",
"https://www.google.com/images/icons/product/test-32.png",
"https://www.google.com/images/icons/product/microsoft-32.png",
"https://www.google.com/images/icons/product/chat-32.png",
"https://www.google.com/images/icons/product/hangouts-32.png",
"https://www.google.com/images/icons/product/maps-32.png",
"https://www.google.com/images/icons/product/map_maker-32.png",
"https://www.google.com/images/icons/product/apple-32.png",
"https://www.google.com/images/icons/product/latitude-32.png",
"https://www.google.com/images/icons/product/sketchup-32.png",
"https://www.google.com/images/icons/product/skymap-32.png",
"https://www.google.com/images/icons/product/google_favicon-32.png",
"https://www.google.com/images/icons/product/mobile_app-32.png",
"https://www.google.com/images/icons/product/mobileapp-32.png",
"https://www.google.com/images/icons/product/goggles-32.png"
];
要检查并记下文档的图像 url,我使用以下方法:当您有权访问https://www.google.com/时,这完全可以正常工作, 因此当人们想要验证这一点时,请使用https上的 js 控制台://www.google.com/
var xhr = {};
for(var i = urls.length; i-- ; i>0){
xhr[i] = new XMLHttpRequest();
xhr[i].open('GET',urls[i]);
xhr[i].onload = write();
function write() {
document.write(urls[i] + "<br>")
}
xhr[i].send(null);
}
现在只写有效的网址(包括png图标的网址。所以chart-32.png
有效,但dropbox-32.png
无效,我尝试了这段代码:
var xhr = {};
for(var i = urls.length; i-- ; i>0){
xhr[i] = new XMLHttpRequest();
xhr[i].open('GET',urls[i]);
xhr[i].onload = write();
function write() {
if(xhr[i].status != 404){
document.write(urls[i] + "<br>")
}
}
xhr[i].send(null);
}
但由于某种原因,只有当我将其限制为有效的 png 时,它才会在 chrome 中出现此错误:错误:InvalidStateError:DOM Exception 11
有谁知道我做错了什么?和/或如何解决这个问题?