img.onload = function() { // the image is ready }; img.onerror = function() { // the image has failed };