desandro / imagesloaded

:camera: JavaScript is all like "You images done yet or what?"
https://imagesloaded.desandro.com
MIT License
8.88k stars 1.15k forks source link

Check if elem is not null #282

Closed xx4159 closed 5 years ago

xx4159 commented 5 years ago

addelementimages

https://github.com/desandro/imagesloaded/blob/master/imagesloaded.js#L136

Check if elem is not null

if ( elem && elem.nodeName == 'IMG' ) {
desandro commented 5 years ago

Thanks for this contribution. Could you provide the use case where this error occurs? Tracing back the code. addElementImages is applied to this.elements. this.elements is set to an array of queryElem. If queryElem is falsey, imagesloaded should return in its constructor. So there's an edge case I'm not accounting for.

xx4159 commented 5 years ago

@desandro queryElem can any array. in my case, I guess... it was imagesLoaded([undefined]).

more details,

html

<div class="feed"></div>

javascript

// ...
const images = Array.prototype.slice.call(document.querySelectorAll('.feed')).map((feed) => feed.getElementsByTagName('img')[0]);
const loaded = imagesLoaded(images);
// ...

and I think I have to fix my code filtering empty feed... :sweat_smile:

desandro commented 5 years ago

You could just do imagesLoaded('.feed') rather than querying, slicing and mapping individual images.