views:

119

answers:

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....

A: 

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: http://stackoverflow.com/questions/318630/get-real-image-width-and-height-with-javascript-in-safari-chrome

Dan Heberden
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