/    Sign up×
Community /Pin to ProfileBookmark

Getting onChange to work with FF and IE

Hi all –

[U]Situation: [/U] I’ve developed a support page where users can drop in help tickets with javascript doing client side testing of the fields for missing data etc and php for server side handling and posting to an email address.
There is also a section to add supporting images, videos etc of the bug/issue using the <input type=”file” /> tag.

Although I could have done server side file size checking, I didn’t want my users to wait half an hour to up load a 10Mb video just to be told it was too large to send via email.

So my client side solution was to have a function fire when a file is selected:

[CODE]<input type=”file” id=”attachment1″ name=”attachment[]” onchange=”findSize(1)” />[/CODE]

The id is name 1 because I have javascript to allow the user to add multiple “Select a file” boxes, dynamically adding 1 to the id number each time. The name is an array for the server side handler of the multiple files (if any). The onchange you see fires “findSize(attachmentRef)”.

[CODE]function findSize(id) {
var maxFileSize = 5242880; //5mb in bytes

var idNumber = id;
var fileInput = $(“#attachment”+idNumber)[0];
if(fileInput.files[0].fileSize > maxFileSize) {
//this file is greater than 5mb – warn the user
alert(“This file exceeds the 5Mb limit – please click Choose File and reselect or compress the file.”);
//set the alert flag so that the user cannot accidentally still upload a large file
document.forms[“send”].elements[“attachmentsGoodToGo”].
value = 0;
}
else {
document.forms[“send”].elements[“attachmentsGoodToGo”].
value = 1;
}
}[/CODE]

This is the findSize function that feeds back to the user instantly if the file is too big for processing – saving headaches all round. I use some jQuery to work out which file input we are looking at and to test the size of the file. If the file is too big, an alert flag is written into the form to prevent the user from hitting submit (one of my client side test conditions for checking the form confirms that this flag is 1 not 0).

[U]Issue:[/U] The code works fine on my test browser – Chrome. But in my next step test (FF 7.0.1 and IE9) nothing happens. I can locate and add a file for upload, but the onchange doesn’t fire. Or is it something in my function that is causing the bug? I’m using jQuery 1.4.4 for your info.

[U]Solution:[/U] I’ve seen many examples of how to implement onchange with different browsers – something about IE and FF differing in what they mean by a change and when it happens; but I admit I’m a bit lost. Can any one spell out a solution ABC fashion?

cheers

frank

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Logic_AliNov 02.2011 — It would be friendlier to use the enclosing form's [FONT=Courier New]onsubmit[/FONT] handler instead, but are you sure those properties are available in I.E. via JQuery?
Copy linkTweet thisAlerts:
@bradfrankshawauthorNov 02.2011 — It would be friendlier to use the enclosing form's [FONT=Courier New]onsubmit[/FONT] handler instead, but are you sure those properties are available in I.E. via JQuery?[/QUOTE]

Hmm not sure am a jquery noobie. That was one solution I had thought of to cycle around the attachments when you submit to check if any are too big if this is what you mean; I just wanted a more user friendly instant update when you select a file, say rather than after selecting 10 files and finding 5 are too big.

cheers

frank
Copy linkTweet thisAlerts:
@Logic_AliNov 02.2011 — Hmm not sure am a jquery noobie. That was one solution I had thought of to cycle around the attachments when you submit to check if any are too big if this is what you mean; I just wanted a more user friendly instant update when you select a file, say rather than after selecting 10 files and finding 5 are too big.

cheers

frank[/quote]
I.E. does support onchange for this element type and it fires when you click the 'Open' button. Do you get any console errors?

This works for Firefox, Opera, Chrome & Safari. On I.E. it just aborts:[CODE]<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<form>
<input type=file onchange='attachmentsGoodToGo.value = findSize( this, 5242880 )'><br>
<input name='attachmentsGoodToGo' value='1'> <!-- Presumably hidden -->
</form>

<script type='text/javascript'>

function findSize( fileInput, maxFileSize )
{
var exceeded = false, bytes;

if( fileInput.files && ( bytes = fileInput.files[0].size ) > maxFileSize)
{
exceeded = true;
alert("At " + bytes + " bytes, this file exceeds the stated size limit of " + maxFileSize + "nnPlease re-select or compress the file.");
}

return exceeded ? 0 : 1;
}

</script>

</body>
</html>[/CODE]
×

Success!

Help @bradfrankshaw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...