Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 0.80 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Image width traces zero with onload when cached
  2. var oImage = new Image();
  3.  
  4. oImage.src = 'http://mydomain.com/image.png';
  5.  
  6. container.html(oImage);
  7.  
  8. oImage.onload = function(){
  9.  
  10.     alert(this.width);
  11. }
  12.        
  13. container.waitForImages(function() {
  14.  
  15.     var cWidth = $(this).width();
  16.  
  17.     alert("width: "+cWidth); // returns 0 - works first time but not cached
  18.  
  19. });
  20.  
  21. // Adding the image to the container for preload
  22.  
  23. container.html('<img src="mygraphic.png" />');
  24.        
  25. var image = $('<img/>', {
  26.     src : 'http://mydomain.com/image.png'
  27. }).load(function () {
  28.     alert(this.width);
  29. })
  30. // maybe clear container before if you want
  31. .appendTo(container);
  32.        
  33. var image = $('<img/>')
  34.     .load(function () {
  35.         alert(this.width);
  36.     })
  37.     .attr('src','http://mydomain.com/image.png')
  38.     .appendTo(container)
  39.     ;