You’ll need a bit more code to prevent the click event, that being to return false from the event.
An easier to manage variation of what you have above, is to move the code to a separate function. I’ve also changed the link to go to the large image, so that people without javascript can see the image, and so that scripting can use that image as well.
Using inline scripting attributes leads to massive amounts of duplication, and ends up making your job a lot harder. I’ve removed them from this sample code, because there is an easier way where we use scripting to attach an onclick event handler on to each of those links.
<div id="gallery">
<img id="im" src="photos/039.jpg">
<a href="photos/001.jpg"><img src="photos/t001.jpg"></a>
</div>
The script code you should place in a separate file, and save it as js/script.js - that way you can load it from the end of your body, without it cluttering up the rest of the HTML code for your page.
...
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>
Now for the scripting code. We want to loop through each of the links in the gallery, and add an onclick event to each one.
js/script.js
var gallery = document.getElementById('gallery'),
links = gallery.getElementsByTagName('a'),
i;
for (i = 0; i < links.length; i += 1) {
links[i].onclick = showPhoto;
}
That showPhoto function, it will have the link available as the this keyword, so that you can easily retrieve the large image from the href attribute.
We can also now return false from the function, to prevent the default action (that of following the clicked on link) from occuring.
function showPhoto() {
document.getElementById('im').src = this.href;
return false;
}