Is it possible to load and show a JPG file using Javascript...?
I have a HTML document, and want to load a new image when the user
pushes a button.
Do you mean you want to change the source (src attribute) of an existing
IMG element when a user presses a button? If so, then yes, it can be done
as long as you accept the fact that a user with JavaScript disabled cannot
use this feature in your page.
Place this somewhere before the button below (HEAD or BODY):
<SCRIPT type="text/javascript">
function changeImage( element, URI ) {
var img = null;
if( document.getElementById ) {
img = document.getElementById( element );
} else if( document.all ) {
img = document.all[ element ];
}
if( img ) {
img.src = URI;
}
}
</SCRIPT>
In the BODY of your document:
<IMG id="giveMeAUniqueID" src="initialImage.jpg">
...
<BUTTON type="button"
onclick="changeImage('giveMeAUniqueID','newImage.jpg')"
replacing the two filenames and the ID name with your own values.
This will only allow the image to be changed once. Cycling between two or
more images, in sequential or random order, is equally possible, but as
you didn't ask for it, you're not getting it[1].
Mike
[1] Obviously, ask if you do need such a feature.