views:

8874

answers:

8


I want to create a simple bit of JS code that creates an image element in the background and doesn't display anything. The image element will call a tracking URL (such as Omniture) and needs to be simple and robust and work in IE 6 =< only. Here is the code I have:

var oImg=document.createElement("img");
oImg.setAttribute('src', 'http://www.testtrackinglink.com');
oImg.setAttribute('alt', 'na');
oImg.setAttribute('height', '1px');
oImg.setAttribute('width', '1px');
document.body.appendChild(oImg);

Is this the simplest but most robust (error free) way to do it? I cannot use a framework like jquery. It needs to be in plain JavaScript.

+5  A: 

Are you allowed to use a framework? jQuery and Prototype make this sort of thing pretty easy. Here's a sample in Prototype:

var elem = new Element('img', { 'class': 'foo', src: 'pic.jpg', alt: 'alternate text' });
$(document).insert(elem);
swilliams
+15  A: 
var img = new Image(1,1); // width, height values are optional params 
img.src = 'http://www.testtrackinglink.com';
Tad
A: 
var img = document.createElement('img');
img.src = 'my_image.jpg';
document.getElementById('container').appendChild(img);
Michael L Perry
Sorry, "appendChild". Teach me not to double-check my code.The answer has been edited.
Michael L Perry
+4  A: 

jQuery:

$('#container').append('<img src="/path/to/image.jpg" width="16" height="16" alt="Test Image" title="Test Image" />');
Darryl Hein
+3  A: 
oImg.setAttribute('width', '1px');

'px' is for CSS only. Use either:

oImg.width= '1';

to set a width through HTML, or:

oImg.style.width= '1px';

to set it through CSS.

Note that old versions of IE don't create a proper image with ‘document.createElement()’, and old versions of KHTML don't create a proper DOM Node with ‘new Image()’, so if you want to be fully backwards compatible 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 slightly wary of document.body.appendChild if the script may execute as the page is in the middle of loading. You can end up with the image in an unexpected place, or a weird JavaScript error on IE. If you need to be able to add it at load-time (but after the element has started), you could try inserting it at the start of the body using body.insertBefore(body.firstChild).

To do this invisibly but still have the image actually load in all browsers, you could insert an absolutely-positioned-off-the-page as the body's first child and put any tracking/preload images you don't want to be visible in there.

bobince
A: 

Shortest way:

(new Image()).src = "http:/track.me/image.gif";
aemkei
If not added to the document, it is not certain the image src will be fetched at all (browser-dependent).
bobince
+1  A: 

As others pointed out if you are allowed to use a framework like jQuery the best thing to do is use it, as it high likely will do it in the best possible way. If you are not allowed to use a framework then I guess manipulating the DOM is the best way to do it (and in my opinion, the right way to do it).

Leandro López
+1  A: 

Just for the sake of completeness, I would suggest using the InnerHTML way as well - even though I would not call it the best way...

document.getElementById("image-holder").innerHTML = "<img src='image.png' alt='The Image' />";

By the way, innerHTML is not that bad

Binny V A