Foros del Web » Programando para Internet » Javascript »

[Aporte] Reemplazar imágenes rotas

Estas en el tema de [Aporte] Reemplazar imágenes rotas en el foro de Javascript en Foros del Web. Hola mi nombre es armando. El otro día navegaba por Internet cuando me encontré con un script muy interesante que sirve para reemplazar imágenes rotas ...
  #1 (permalink)  
Antiguo 27/03/2013, 17:50
Avatar de Armando99  
Fecha de Ingreso: marzo-2013
Ubicación: En una casa
Mensajes: 13
Antigüedad: 11 años, 1 mes
Puntos: 1
Información [Aporte] Reemplazar imágenes rotas

Hola mi nombre es armando.

El otro día navegaba por Internet cuando me encontré con un script muy interesante que sirve para reemplazar imágenes rotas (Que no se muestran), y me pareció buena idea compartirlo con ustedes.

Código Javascript:
Ver original
  1. <script type='text/javascript'>
  2. //<![CDATA[
  3. // Script para sustituir imágenes rotas
  4. function ImagenOk(img) {
  5. if (!img.complete) return false;
  6. if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) return false;
  7. return true;
  8. }
  9. function RevisarImagenesRotas() {
  10. var replacementImg = "AQUI LA URL DE LA IMAGEN DE REEMPLAZO";
  11. for (var i = 0; i < document.images.length; i++) {
  12. if (!ImagenOk(document.images[i])) {
  13. document.images[i].src = replacementImg;
  14. }}}
  15. window.onload=RevisarImagenesRotas;
  16. // ]]>
  17. </script>

Espero que les funcione lo mas probable es que ustedes ya sepan como hacerlo pero habrá otros que no saben o no se les ocurrió hacer.
  #2 (permalink)  
Antiguo 27/03/2013, 21:40
Avatar de emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: [Aporte] Reemplazar imágenes rotas

En realidad la propiedad complete determina si una imagen se termina de cargar, no si existe o no. lo apropiado sería utilizar onerror, de lo contrario alguna demora en la imagen no cargada puede provocar comportamientos impredecibles, sobre todo en IE.
Algo asi

Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <title>Reemplazar imágenes que no existen</title>
  3. <meta charset="utf-8" />
  4. <style type="text/css">
  5. img{
  6. border: solid 1px #000;
  7. }
  8. <script type="text/javascript">
  9. window.onload = function(){
  10. var imagenes = [];
  11. imagenes = document.getElementsByTagName('img');  
  12. for (i=0; i<imagenes.length; i++) {
  13. reemplazar(imagenes[i]);
  14. }
  15. }
  16.  
  17. function reemplazar(ima){
  18. var reemplazo = 'https://googledrive.com/host/0B5cDQ-G3aVMQMkdaelNsdTZ6TDQ/img/homer-car.gif'// imagen de reemplazo
  19. var ImagenPrueva = new Image();
  20. ImagenPrueva.src = ima.src;
  21. ImagenPrueva.onerror = function(){
  22. ima.src = reemplazo;
  23. ima.title = "No diponible";
  24. }
  25. }
  26. </head>
  27. <p>Las imágenes 3ª y 7ª no existen</p>
  28. <div>
  29. <img src="https://googledrive.com/host/0B5cDQ-G3aVMQMkdaelNsdTZ6TDQ/isabelM/img/forrest-gump-250x350.jpg" alt="" width="150" height="210" />
  30. <img src="https://googledrive.com/host/0B5cDQ-G3aVMQMkdaelNsdTZ6TDQ/isabelM/img/gladiador-250x350.jpg" alt="" width="150" height="210" />
  31. <img src="https://googledrive.com/host/0B5cDQ-G3aVMQMkdaelNsdTZ6TDQ/isabelM/img/xxxxx.jpg" alt="" width="150" height="210" />
  32. <img src="https://googledrive.com/host/0B5cDQ-G3aVMQMkdaelNsdTZ6TDQ/isabelM/img/piratas-del-caribe-250x350.jpg" alt="" width="150" height="210" /><br />
  33. <img src="https://googledrive.com/host/0B5cDQ-G3aVMQMkdaelNsdTZ6TDQ/isabelM/img/casablanca-250x350.jpg" alt="" width="150" height="210" />
  34. <img src="https://googledrive.com/host/0B5cDQ-G3aVMQMkdaelNsdTZ6TDQ/isabelM/img/lo-que-el-viento-250x350.jpg" alt="" width="150" height="210" />
  35. <img src="https://googledrive.com/host/0B5cDQ-G3aVMQMkdaelNsdTZ6TDQ/isabelM/img/zzzzz.jpg" alt="" width="150" height="210" />
  36. <img src="https://googledrive.com/host/0B5cDQ-G3aVMQMkdaelNsdTZ6TDQ/isabelM/img/impala-600x375.jpg" alt="" width="150" height="210" /><br />
  37. <img src="https://googledrive.com/host/0B5cDQ-G3aVMQMkdaelNsdTZ6TDQ/img/steel6.jpg" alt="" width="150" height="210" />
  38. </div>
  39. </body>
  40. </html>

Igual, buena info

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.

Etiquetas: reemplazar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 01:38.