tags:

views:

157

answers:

3

How do I automatically open the browse dialog of an input type="file" when the page first opens?

+1  A: 

fire the click event on the button on document ready

$(document).ready(function() {
    $("#buttonid").click();
});
GerManson
Let's not make outlandish assumptions, now. Try `document.getElementById` instead.
icio
Doesn't work - http://jsbin.com/akure - least not in Firefox...
gnarf
@gnarf: It works in IE8 and Chrome.
SLaks
it also works on chrome, let me see if i can fix it for firefox
GerManson
If the OP added jQuery then this might work.
James Westgate
+1  A: 

This is completely impossible in Firefox.

In other browsers, you can:

document.getElementById('inputId').click();
SLaks
Please tell us why.
Diodeus
@Diodeus: Because Firefox doesn't support it.
SLaks
And in IE, forms whose file upload buttons have been clicked by script won't submit properly either. The whole idea is basically a dead loss.
bobince
+7  A: 

I don't think you should do this. If you've ever visited a MySpace page, you know how frustrating it can be when a web page activates things on its own when the page loads.

Don't violate how the UI is supposed to work, let the user ask for the dialog.

Besides, if the users instinctively closes it like a popup, and then realizes they needed it, it may not be obvious how they should get the dialog back. Then they will reload the page just to show the dialog again -- all frustrating things you could be avoiding.

That being said, I'm not sure why you want to do this in the first place. This is just my first reaction to what you're asking.

Carson Myers
I totally agree with you Carson
GerManson