oImg.setAttribute('width', '1px');
px is for CSS only. Use either:
oImg.width = '1';
to set the width via HTML, or:
oImg.style.width = '1px';
to set it through CSS.
Note that older versions of IE do not create the correct image using document.createElement() , and older versions of KHTML do not create the proper DOM Node with new Image() , so if you want to be fully compatible with feedback, use something like :
// IEWIN boolean previously sniffed through eg. conditional comments function img_create(src, alt, title) { var img = IEWIN ? new Image() : document.createElement('img'); img.src = src; if ( alt != null ) img.alt = alt; if ( title != null ) img.title = title; return img; }
Also be careful with document.body.appendChild if the script can be executed as the page is in the middle of loading. You may receive an image in an unexpected place or a strange JavaScript error in IE. If you need to add it at boot time (but after running the <body> element), you can try inserting it at the beginning of the body using body.insertBefore(body.firstChild) .
To make this invisible, but still actually load the image in all browsers, you can insert an absolutely-placed-out-of-page <div> as the body of the first child and put any tracking / preload images that you don’t use want to be visible there.
bobince Oct 22 '08 at 18:49 2008-10-22 18:49
source share