SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Location
    UK
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    doing something after a user selects a file

    If i have a form element to choose a file:

    <input type="file" name="blah">

    Does anyone know who to call a JavaScript function immediately after someone has chosen a file? onChange doesnt work.

    Thanks
    -AT

  2. #2
    pg for mild peril cow's Avatar
    Join Date
    Mar 2000
    Location
    nevada, usa
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you tried onClick?
    "There's no justice like angry mob justice!" --Seymour Skinner

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Location
    UK
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah but that happens before the user selects a file

    thanks anyway
    -AT

  4. #4
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't, for security reasons. Browsers do not allow manipulation of the file upload element in any way using javascript or CSS - you can't chankge how it looks, you can't access its contents with javascript and you can't assign event handlers to it. This is to prevent people from abusing it and using clever bits of code to trick people in to uploading files from their PC that they did not mean to upload.

  5. #5
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    onchange works for me in IE6.

    Code:
    <html>
    <head>
    	<title>upload</title>
    </head>
    <body>
    
    <input type="file" onchange="alert('Hello');">
    
    </body>
    </html>

  6. #6
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does? Wow, didn't think they even allowed that kind of scripting. I'm sure you can't access the contents of the field or change it's appearance with CSS though - looks like I over-emphasized the strictness with which browsers treat that particular element.

  7. #7
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only security restriction on the <input type="file"> element is that the .value property is read only. Otherwise, everything else works as expected - you can even hide the field and trigger the file browser via javascript from another button, link, event, etc....!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Location
    UK
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks everyone it seems that <input type="file" onchange="alert('Hello');"> doesnt work in IE5.0 though. Will just have to find another way.
    -AT

  9. #9
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn that IE5....!

    Is it crucial that you perform the check directly after the file is selected? Presumably this is for validation of the selected filetype, so couldn't you simply run the check in a function called by the onSubmit event of the form?
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Location
    UK
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wanted to submit the form immediately after a picture was selected. I tried hiding the file input and using another button but the click() method for file inputs doesn't work in NS6. So frustrating - no onClick in IE, no click() in NS. Anyway I'll probably just let the user select the file and then press another button to submit.

    Cheers
    -AT


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •