Hi Alluvian,
As for which is better (new Image() or document.createElement), I don't know. The following is a quick test. Both methods worked for me in Opera 9.10, Firefox 2.0 and IE 7, 6 and 5.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Template</title>
<style type='text/css'>
p {
border: 1px solid black;
}
.icons {
width: 24px;
height: 24px;
border: none;
margin-right: 1em;
}
</style>
<script type='text/javascript'>
window.onload = function()
{
createIcons();
};
function createIcons()
{
var img, i = 1, e = document.getElementById('para' + i);
while (e) {
img = document.createElement('img');
//img = new Image();
img.className = 'icons';
img.src = 'icon' + i + '.png';
e.insertBefore(img, e.firstChild);
e = document.getElementById('para' + (++i));
}
}
</script>
</head>
<body>
<p id="para1">bla bla bla</p>
<p id="para2">bla bla bla</p>
</body>
</html>
But the real question is where in your document do you want the images? In my demo above I did it in a way that did not require positioning. But it depends on your document and where you want the images.
Bookmarks