Click to See Complete Forum and Search --> : Cannot open an image with a # sign in name


davidxz
09-03-2003, 06:00 PM
I have ASP file (ViewImage.asp) that uses a JavaScript function window.open to open and view image files:

<html><body>

<script language="javascript">
function viewImg(d){
var fname = d[d.selectedIndex].value;
var msg=window.open('SampleImages/' + fname,'msg','width=350,height=350,scrollbars,resizable')
}
</script>

<form>
<select name="myImages">
<option value="Sample 1.jpg">Sample 1</option>
<option value="Sample 2.jpg">Sample 2</option>
<option value="Room # 1 Lab.jpg">Room # 1 Lab</option>
<option value="Room_#_1_Lab.jpg">Room_#_1_Lab</option>
</select>

<br>
<input type="button" value="View" class="buttons" onclick="JavaScript: viewImg(this.form.myImages)">
</form>

</body></html>


The dropdown list was populated from database with image file names.

In this example, I can view images "Sample 1.jpg" and Sample 2.jpg", but I cannot open "Room # 1 Lab.jpg" or "Room_#_1_Lab.jpg" because there is # sign in the file name.

In this project the files are from the database and I am not authorized to rename images files, so I have to deal with this type of file name. How can I do it? Please give an advice!
Thanks,
David

pyro
09-03-2003, 06:28 PM
You need to escape the special characters, like this:

<script type="text/javascript">
function viewImg(d){
var fname = escape(d[d.selectedIndex].value);
var msg=window.open(escape(fname),'msg','width=350,height=350,scrollbars,resizable,location=1');
}
</script>That will turn characters other then ASCII letters, digits and certain punctuation (@,*,_,+,-,., and /) to their hexadecimal equivilants (%23 for a #)