Ver Mensaje Individual
  #4 (permalink)  
Antiguo 13/02/2013, 19:55
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: Problema al recuperar el width de una imagen

Cita:
Iniciado por chichote Ver Mensaje
gracias master, vamos a leer sobre lo que me dices.

Saludos y gracias nuevamente.
Tu código presta a confusiones, estas creando una imagen dinamicamente con js, pero en el src le asignas como fuente el title de un elemento de id #imagen, que supongo NO es la imagen que creas, si no hay title, no hay new Image(), y si efectivamente #imagen es la imagen de la que querés obtener las dimensiones, deja de tener sentido el new Image(), a ver si con este ejemplo me explico

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>titulo</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  7. <script type="text/javascript">
  8. //<![CDATA[
  9.  
  10. var img = new Image();
  11. img.onload = function() {
  12.  alert('método 1: ' + this.width + 'x' + this.height);
  13. }
  14. img.src = 'fuente_ff.jpg';
  15.  
  16. function metodo2(){
  17. var ancho = $("#uno").width(); 
  18. var alto = $("#uno").height(); 
  19. alert('método 2: ' + ancho + 'x' + alto);
  20. }
  21.  
  22. function metodo3(){
  23. var img = document.getElementById('uno');
  24. //or however you get a handle to the IMG
  25. var w = img.clientWidth;
  26. var h = img.clientHeight
  27. alert('método 3: ' +w + 'x' + h);
  28. };
  29. //]]>
  30.  
  31.  
  32. </head>
  33.     <p>método 1 - se crea la imagen dinamicamente con js y tras la carga se detectan ancho y alto</p>
  34.     <button onclick="metodo2()">método 2 - jQuery</button><br />
  35.     <button onclick="metodo3()">método 3 - Js puro</button><br />
  36. <img src="fuente_ff.jpg" alt="" id="uno"/>
  37. </body>
  38. </html>


Con el primer método, aún quitando el tag <img> lo detectarías.
Me explico?

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