admin管理员组文章数量:1022249
So I need a function to test if an image can load or not. Here's what I've got:
function imgExists(url){
var img = new Image();
img.onerror = function(){
alert('error')
}
img.onload = function (){
alert('load')
}
img.src = url;
}
imgExists('/[email protected]')
The issue is that the alerts are trapped in their won functions. How can I get the imgExists()
function to return true if the image loads and false if it does not?
So I need a function to test if an image can load or not. Here's what I've got:
function imgExists(url){
var img = new Image();
img.onerror = function(){
alert('error')
}
img.onload = function (){
alert('load')
}
img.src = url;
}
imgExists('http://hashtraffic./img/[email protected]')
The issue is that the alerts are trapped in their won functions. How can I get the imgExists()
function to return true if the image loads and false if it does not?
- 6 The image will load asynchronously, so you cannot return a value immediately from the function. You will have to follow the deferred execution pattern and either provide a callback for your function to call, or have it return a Deferred object. – Frédéric Hamidi Commented Jun 21, 2012 at 12:32
- Any examples? How do I provide a callback? – alt Commented Jun 21, 2012 at 12:34
- You pass it as parameter to your function and call it inside the function when you have the result. – Felix Kling Commented Jun 21, 2012 at 12:35
1 Answer
Reset to default 6You need to use a callback, since your function is inherently asynchronous:
function imgExists(url, callback) {
var img = new Image();
img.onerror = function() {
callback(false);
}
img.onload = function () {
callback(true);
}
img.src = url;
}
function checkImage(exists) {
alert("Image exists: " + exists); // Usage example.
}
imgExists('http://hashtraffic./img/[email protected]', checkImage);
So I need a function to test if an image can load or not. Here's what I've got:
function imgExists(url){
var img = new Image();
img.onerror = function(){
alert('error')
}
img.onload = function (){
alert('load')
}
img.src = url;
}
imgExists('/[email protected]')
The issue is that the alerts are trapped in their won functions. How can I get the imgExists()
function to return true if the image loads and false if it does not?
So I need a function to test if an image can load or not. Here's what I've got:
function imgExists(url){
var img = new Image();
img.onerror = function(){
alert('error')
}
img.onload = function (){
alert('load')
}
img.src = url;
}
imgExists('http://hashtraffic./img/[email protected]')
The issue is that the alerts are trapped in their won functions. How can I get the imgExists()
function to return true if the image loads and false if it does not?
- 6 The image will load asynchronously, so you cannot return a value immediately from the function. You will have to follow the deferred execution pattern and either provide a callback for your function to call, or have it return a Deferred object. – Frédéric Hamidi Commented Jun 21, 2012 at 12:32
- Any examples? How do I provide a callback? – alt Commented Jun 21, 2012 at 12:34
- You pass it as parameter to your function and call it inside the function when you have the result. – Felix Kling Commented Jun 21, 2012 at 12:35
1 Answer
Reset to default 6You need to use a callback, since your function is inherently asynchronous:
function imgExists(url, callback) {
var img = new Image();
img.onerror = function() {
callback(false);
}
img.onload = function () {
callback(true);
}
img.src = url;
}
function checkImage(exists) {
alert("Image exists: " + exists); // Usage example.
}
imgExists('http://hashtraffic./img/[email protected]', checkImage);
本文标签: javascriptTurn an image39s onError function into a returnStack Overflow
版权声明:本文标题:javascript - Turn an image's onError function into a return - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745564170a2156348.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论