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

Safeguard to check if jQuery.fn and jQuery.Deferred exists before using... #265

Closed aolin480 closed 6 years ago

aolin480 commented 6 years ago

Error from 3rd Party script using imagesLoaded:

imagesloaded.pkgd.js:480 Uncaught TypeError: Cannot set property 'imagesLoaded' of undefined
    at Function.ImagesLoaded.makeJQueryPlugin (imagesloaded.pkgd.js:480)
    at factory (imagesloaded.pkgd.js:486)
    at imagesloaded.pkgd.js:149
    at imagesloaded.pkgd.js:155

We are trying to improve our pageload times due to render-blocking jQuery, and this error just came up for us from an external script that is using imagesLoaded.

In our implementation we are defining our own jQuery global in the header of our document to catch all inline jQuery commands in the body, and then towards the footer, after the actual jQuery library is loaded (which is async/deferred too) we are processing those catches. Since we're only catching a few common jQuery objects used on our platform (ready, on, bind, click), we are not including $.fn or $.Deferred so imagesLoaded is actually looking at our jQuery global and errors out because it cannot find those functions.

I know this is a rare case of using jQuery, but our platform is full of clients with some inline javascript in areas we just cannot bulk update and change in a snap. I believe there should be a check if $.fn and $.Deferred exists before using them. If, in a different world, jQuery removes these functions, imagesLoaded would break, so I think checking before using would be ideal.

desandro commented 6 years ago

Thanks for this contribution and for detailing your situation.

As with #259, I feel these checks are for too small of an edge case. I'd rather rely on dependency semantic versioning over feature checks in code.

I appreciate your effort here, but I'll have to pass on this one.