2

A sample of my code is here: http://develop.davzy.com/test2.php

<input name="avatarfile" type="file" id='uploadbutton' />
<script>document.getElementById("uploadbutton").click()</script>

It is supposed to open the file selector as soon as you open the page. It works in IE8, IE9, FireFox, but not in Chrome.

David Zorychta
  • 13,039
  • 6
  • 45
  • 81
  • 1
    Firefox 7 also blocks it. It must be a security feature. I would hate sites opening my "browse files" window. Inexperienced web users can easily upload a file, without realizing. – Emil Oct 22 '11 at 04:07
  • 2
    duplicate http://stackoverflow.com/questions/210643/in-javascript-can-i-make-a-click-event-fire-programmatically-for-a-file-input – Emil Oct 22 '11 at 04:12
  • The reason I want this is because I want it so when the user pushes "upload image" it will open this im the background, and I wanted it to open the upload dialog. – David Zorychta Oct 22 '11 at 13:45

2 Answers2

2

It won't work, not even in Firefox (7+). It's a security measure to avoid hijacking of the input field.

Regarding the pop up, Firefox warns you that the page is trying to pop up a window (the file upload dialog is an ui window) on page load, so the user is notified and is given the choice to allow it or block it.

Anyway, why would you need to open an file dialog on page load?

xmarcos
  • 3,298
  • 2
  • 20
  • 27
1

is because the browser blocks pop-ups, at least in firefox tells me a pop-up blocking

elios264
  • 385
  • 2
  • 16