I use the below code to display an image to my client when they have browsed to it. The image folder is in another location but always the same. My problem is that when i browse to the image, this code works fine, however, if i just paste the filename in, it does not display becuase the path is always incorrect.
I'm sure it's just a logic problem but today is one of those days and i just don't see the solution.
TIA
Function DisplayImage(ImageName,ElementValue){
document.images[ImageName].src= http://codingforums.com/archive/index.php/ElementValue;
}
<img src=http://codingforums.com/archive/index.php/"../Images/6_1/Blank_image.jpg" name="preview_h" width="103" height="112" id="preview_h">
<input name="headline_photo" type="File" id="headline_photo" size="17" onChange="DisplayImage('preview_h',this.value)">
pccode
11-24-2005, 06:34 AM
I'm not sure if you're talking about inserting an image locally from the client computer, but I rewrote the code so that you could test it locally.
<html>
<head>
<script>
function DisplayImage(ImageID,ElementValue) {
var temp = ElementValue.replace(/\\/g,"/");
temp = "file:///" + temp;
document.getElementById(ImageID).setAttribute('src',temp);
}
</script>
</head>
<body>
<img src=http://codingforums.com/archive/index.php/"reply.gif" name="preview_h" width="103" height="112" id="preview_h">
<form onreset="window.location.reload()">
<input name="headline_photo" type="File" id="headline_photo" size="17" onChange="DisplayImage('preview_h',this.value)">
<br><br>
<input type="reset" value="reset">
</form>
</body>
</html>
With a local file you need to add "file:///" to the beginning of the url and change all of the backslashes to forslashes. In FF it does not automatically activate the onchange event when you paste text into an input box. You need to hit enter to activate the event handler. I have no idea about IE since I haven't tested it with this code. One other note, if you do decide to paste a local file url into the input box, don't worry about reformatting it since the code above will do it for you.
tcadieux
11-28-2005, 07:36 PM
Thank you, this does work in certain instances.
However, i think the easiest thing for me to do would be to strip all information save for the file name and then display the file at a hardcoded location.