jQuery: i problemi relativi al caricamento delle immagini

Il caricamento delle immagini presenta in jQuery dei problemi specifici.

jQuery di Gabriele Romanato

Il caricamento delle immagini presenta in jQuery dei problemi specifici.

L'evento load() di jQuery è stato deprecato ufficialmente a causa di vari problemi sorti durante l'implementazione e, soprattutto, delle differenti interpretazioni date dai vari browser. Vediamo in dettaglio questo problema.

La documentazione ufficiale riporta ad esempio i seguenti problemi dell'evento load() con le immagini:

A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:

  • It doesn't work consistently nor reliably cross-browser
  • It doesn't fire correctly in WebKit if the image src is set to the same src as before
  • It doesn't correctly bubble up the DOM tree
  • Can cease to fire for images that already live in the browser's cache

L'evento non funziona in modo cross-browser (specie nei browser basati su WebKit), non attiva correttamente la fase di bubble (risalita) dell'evento nell'albero del DOM e può non funzionare quando le immagini sono presenti nella cache del browser.

Esempio:


$( "#cover" ).load(function() {
  // ...
});

Per i motivi sopra elencati se ne sconsiglia l'uso.

Commenti