Created
March 14, 2017 16:12
-
-
Save welll/e4bbf930e9befaa91a891f67647db85e to your computer and use it in GitHub Desktop.
Checking if the image was loaded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkIfImageExist(src, cb) { | |
var img = new Image(); | |
img.onload = function() { | |
cb(null) | |
} | |
img.onerror = function(e) { | |
cb(new Error('Image ' + src + ' doesnt exist')) | |
} | |
img.src = src | |
} | |
document.querySelectorAll(`.logo-${size}`).forEach((item) => { | |
let backgroundImage = window.getComputedStyle(item)['backgroundImage'] | |
backgroundImage = backgroundImage.replace(/url\("/g, '').replace(/"\)/g, '') | |
if (backgroundImage.indexOf('data:') !== -1) { | |
return | |
} | |
checkIfImageExist(backgroundImage, (err) => { | |
if (err) { | |
let div = document.createElement('div') | |
div.innerHTML = backgroundImage; | |
body.append(div) | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment