Javascript:如何延迟为img.complete返回值 [英] Javascript: How can I delay returning a value for img.complete
问题描述
我已经编写了一个脚本来测试IMG标签中对SVG的支持:
I've written a script to test for SVG support in the IMG tag:
function SVGinIMG() {
var SVGdata = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNzUiIGhlaWdodD0iMjc1Ij48L3N2Zz4%3D'
var i = document.createElement('img');
i.setAttribute('src',SVGdata);
return i.complete;
}
window.onload = function() {
var hasSVG = SVGinIMG();
alert(hasSVG);
}
这就是我想要的,除了在WebKit浏览器中运行脚本时, complete 属性不会在我第一次加载页面时触发;而是在第一次加载页面时触发.当我刷新页面时,它可以正常工作. return 函数在img完成加载之前正在运行;延迟此操作的最佳方法是什么?
This does what I want, except that when I run the script in WebKit browsers the complete property doesn't trigger the first time I load the page; when I refresh the page, it works as it should. The return function is running before the img has finished loading; what's the best method to delay this?
推荐答案
我有点使事情复杂化了.我真正需要的只是 load 事件:
I was complicating things a little; all I really needed was the load event:
function SVGDetect() {
var testImg = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNzUiIGhlaWdodD0iMjc1Ij48L3N2Zz4%3D';
var img = document.createElement('img')
img.setAttribute('src',testImg);
img.addEventListener('load',setCSS,true);
}
此功能会在加载图片时运行另一种功能,而对于不支持图片中SVG的浏览器则不会.
This runs another function when the image loads, which is never in the case of browsers that don't support SVG in images.
这篇关于Javascript:如何延迟为img.complete返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!