1

The simplified version of the problem I am seeing in IE7 can be demonstrated using FireBug Lite.

On a page loaded with jQuery, I open FireBug Lite (via bookmarket) and I enter the following:

image = $('<img src="http://example.com/boofar.jpg" border="12" 
                    width="95" height="95" title="Booya">')[0];

and the result echoed is:

<img title="Booya" contentEditable="inherit" start="fileopen" 
                    loop="1" src="http://example.com/boofar.jpg" border="12">

Where are the width and height attributes? Furthermore,

 image.width;

and

 image.attributes.width.value;

return 0 and "0".

Seen this with both jQuery 1.2.6 as well as 1.4.2. It does the right thing in IE8 and FF.

Any ideas where those attributes went? Very annoying....

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
  • you can do table = $('
    ')[0] and get so what gives?
    – Scott Evernden Mar 31 '10 at 17:44
  • @Scott - I'm not seeing this behavior: http://jsfiddle.net/UGzED/ I get: `` for the result. – Nick Craver Mar 31 '10 at 17:47
  • @Nick - huh - verrrry interesting . lemme study what's different cause I don't get it.. seems if you insert the img into the div, the attrs re-appear ? .. weirdness .. tnx for commenting – Scott Evernden Mar 31 '10 at 18:05
  • Okay i'm even more baffled .. in my example, image.outerHTML gives but image.width gives 0 .. !!?wtf!? .. btw @Nick - thanks for the jsfiddle resource / useful! – Scott Evernden Mar 31 '10 at 18:26
  • @Scott - Elements don't get height/width until inserted into the DOM, things like padding or other style rules affect the value, so what the height is depends on where the element is inserted. – Nick Craver Mar 31 '10 at 18:35
  • i want to to get the value of width and height attribute values as specified in the HTML. I am not interested in the width / height of the loaded image - and I agree I won't know THOSE values til the image has been fetched. But those *attribute* values MUST be maintained *somewhere* in the element irregardless of whether the src has been loaded. As far as I can tell digging into the DOM they seem to be held solely in the outerHTML field and not maintained anywhere else ... – Scott Evernden Mar 31 '10 at 18:45

1 Answers1

1

You'll get better results using jQuery to create the image attributes directly:

var $image = jQuery('<img />', 
       {   
           title: "Booya",
           src:   "http://example.com/boofar.jpg",
           css: {
                  border: "12px",
                  width : "95px",
                  height: "95px"
                }
      });

You'll run into issues obtaining correct width/height with webkit browsers when it's set explicitly vs. it's actual width/height. You might want to take a peek at this: Get the real width and height of an image with JavaScript? (in Safari/Chrome)

Community
  • 1
  • 1
Dan Heberden
  • 10,990
  • 3
  • 33
  • 29
  • well yeah but -- this simplified problem represents a issue I am seeing in a generalized HTML analyzer I've built. I am not specifically needing to make IMAGES by themselves. Instead I am using jQuery to wrap an arbitrary HTML string and then traversing the result set to figure the structure out.. – Scott Evernden Mar 31 '10 at 18:26