473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Preview image before upload.


I have a form to allow the user upload images.
The user browse for an image in their computer and click open, th
image to be upload is preview on the img tag.
The script works fine with IE, but does not work with mozilla Netscap
bowsers.
Does anyone knows what could be the problem?
Does the ONchange function works in Netscape?
Or anyone knows a tag that can preview the image file before uploadin
it.

<script language="JavaScript" type="text/JavaScript">
function preview(thisImg,thisObj)
{

var filename = "";
var nothing = "";
filename = "file:\/\/"+thisObj.value;

var fileExtension
filename.substring(filename.lastIndexOf(".")+1);
if (fileExtension == "jpg" || fileExtension == "jpeg" |
fileExtension == "gif")
{ thisImg.src="file:\/\/"+thisObj.value; }

else
{ alert ("Only jpg, jpeg and gif file
are allowed."); }

}
</script>

<form name="form2" method="post" enctype="multipart/form-data">

<img src="images/default.gif" name="myimga" width=100 height=8
border=0>

<INPUT NAME="upfile" TYPE="file" SIZE="80
ONchange="preview(myimga,upfile);">

<INPUT NAME="upload" TYPE="submit" VALUE="Upload">

</form
-
miart

Jul 20 '05 #1
4 32214
On Thu, 26 Feb 2004 13:48:08 -0600, miarte
<mi***********@mail.forum4designers.com> wrote:
I have a form to allow the user upload images.
The user browse for an image in their computer and click open, the
image to be upload is preview on the img tag.
The script works fine with IE, but does not work with mozilla Netscape
bowsers.
Does anyone knows what could be the problem?
Does the ONchange function works in Netscape?
Strange capitalisation, there.
Or anyone knows a tag that can preview the image file before uploading
it.
The first problem is that you are referring to elements as though their
names are global identifiers. They are not - it is an IE thing (though
some other browsers might imitate it). Use the appropriate collections
(addressed later).

Another problem is that the onchange event won't fire properly in all
browsers. In IE and Opera, it works fine. In Netscape, you have to lose
focus on the control before it updates the image, and that seemed somewhat
tempermental. In Mozilla, nothing happens. The best approach, in my
estimation, would be to use a separate button that updated the preview,
rather than doing it automatically.
<script language="JavaScript" type="text/JavaScript">
The language attribute is deprecated - remove it. The type attribute is
sufficient.

[snipped function]
</script>

<form name="form2" method="post" enctype="multipart/form-data">
You should really use better names than "form2", and the like. It's good
programming practice to use descriptive names. The same goes for "myimga",
below.
<img src="images/default.gif" name="myimga" width=100 height=80
border=0>
The name attribute on IMG elements should only be used for
backward-compatibility. If NN4 support isn't an issue, use the id
attribute.
<INPUT NAME="upfile" TYPE="file" SIZE="80"
ONchange="preview(myimga,upfile);">
Move this to an onclick event on a separate 'preview' button. You'll also
have to change the call to:

preview(document.images['imageName'],document.formName.upfile);

where imageName and formName are the new names of the elements, if you
decide to change them.

For future reference: in the original call, you should have used:

preview(document.images['myimga'],this);
<INPUT NAME="upload" TYPE="submit" VALUE="Upload">

</form>


Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
Ivo
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...

<img src="images/default.gif" name="myimga" width=100 height=80
border=0>


The name attribute on IMG elements should only be used for
backward-compatibility. If NN4 support isn't an issue, use the id
attribute.


Is that so? I find the document.images collection still very well supported
among the curent browsers. So much in fact, that document.images["foo"] is
always much faster than a document.getElementById("foo") would be in the
same situation, plus it is less code, it is easier to type, and it reminds
of the nature of "foo".
FWIW
Ivo
Jul 20 '05 #3
"Ivo" <no@thank.you> writes:
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
The name attribute on IMG elements should only be used for
backward-compatibility. If NN4 support isn't an issue, use the id
attribute.

Is that so? I find the document.images collection still very well supported
among the curent browsers.
It should be. It is part of the W3C DOM.
So much in fact, that document.images["foo"] is always much faster
than a document.getElementById("foo") would be in the same
situation, plus it is less code, it is easier to type, and it
reminds of the nature of "foo".


Yes, it is great. And it works with "id" too in modern browsers and
IE, so the advice to use "id" instead of "name" is not affected by
using document.images, only by using Netscape 4 or other ancient
browsers.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
Ivo
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:oe**********@hotpop.com...
So much in fact, that document.images["foo"] is always much faster
than a document.getElementById("foo") would be in the same
situation, plus it is less code, it is easier to type, and it
reminds of the nature of "foo".
Yes, it is great. And it works with "id" too in modern browsers and
IE, so the advice to use "id" instead of "name" is not affected by
using document.images, only by using Netscape 4 or other ancient
browsers.


Aha, click, OK.
Thanks, Ivo

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DH | last post by:
I have a "file upload form" that works OK, but I have been unsuccessful in my attempt to also resize the uploaded .JPG (if it is too wide), over-writing the original .JPG, and then create and save...
0
by: Paul Hamlington | last post by:
Hello, I've been programming in ASP for a little while now and quite an advanced user, but I have come across an unusual problem in which I need assistance. I have built my own image upload,...
2
by: Faree | last post by:
Hi, I am a PHP Developer.can any one help me on image upload and display of that image.what i want is uploading an image to a directory ,displaying of that image as a thumbnail.when i...
3
by: Michael | last post by:
Hello, I want to write a componant that helps a user upload an image or file. I want something like you see on yahoo's image upload page. It has to work on mac and windows. Can anyone help me get...
1
pritipshah
by: pritipshah | last post by:
Hi All, I am using vtiger open source CRM and in that they are using FCKeditor for Image Upload (Resource browser window) but it is not upload or make new folder for image. Vtiger CRM...
7
by: mishrarajesh44 | last post by:
hii all Truly telling i hav got this code from net & i am finding error while running the code below.. code:- <?php $idir = "photo/"; // Path To Images Directory $tdir =...
7
by: dragiton | last post by:
SA Upload SQL Database variable types (image upload and storage) I am having trouble with the SA Upload utility. The following code used to work correctly. However, I lost my database and had to...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
26
matheussousuke
by: matheussousuke | last post by:
May someone help me correct this script? there a few ' and " and ; in the wrong places: <?php include "./comm.inc"; connectdb(); $sql = "SELECT imgid,imgtype FROM...
3
by: leelotis | last post by:
hey im trying to find a decent image upload preview and resize (for images that dont fit the placed view format) ive looked all over the web and ive had no luck and ideas?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.