MrZ
MrZ

Reputation: 123

HTML5 canvas image object don't work


I'm using a function to return an image object with HTML5 canvas, but I always have an error alert (onerror event)

function GetThatImage() {
    var image = new Image();
    image.src = "http://localhost/assets/images/loadedsprite.png";
    image.onerror = alert('Oops... Image isn\'t loaded: '+image.src);
    return image;
}

Can anyone help me ?
Thanks
(sorry for my bad english)

EDIT: with this code, I have first an alert 'oops image is not loaded...' then I have 'image loaded'

function GetThatImage() {
    var image = new Image();
    image.src = "http://localhost/assets/images/loadedsprite.png";
    image.onload = function() {
        alert('image loaded');
    };
    image.onerror = alert('Oops... Image isn\'t loaded: '+image.src);
    return image;
}

Upvotes: 2

Views: 77

Answers (1)

R3tep
R3tep

Reputation: 12854

You need to set the event onerror with an anonymous function like image.onload.

image.onerror = function () { 
    alert('Oops... Image isn\'t loaded: '+image.src);
}

Upvotes: 1

Related Questions