I have an <input type="file" id="browse-button"/>
file-browser input in my HTML.
I have another button with ID choose-file-button
that, when clicked, calls document.getElementById("browse-button").click();
. When this button is clicked, it correctly clicks #browse-button
and the file dialog opens.
Now, I took code from this answer to intercept a Ctrl+O
keypress and open my file dialog, so I have this:
$(window).bind('keydown', function(e)
{
if (e.ctrlKey || e.metaKey)
{
switch (String.fromCharCode(e.which).toLowerCase())
{
case 's':
e.preventDefault();
// doesn't matter for this question
return false;
case 'o':
e.preventDefault();
document.getElementById("choose-file-button").click();
return false;
}
}
return true;
});
As you can see, when I intercept Ctrl+O
I click on my #choose-file-button
button, which calls document.getElementById("browse-button");
in its onclick
handler. I have put a breakpoint in this click handler, and when I press Ctrl+O
it does arrive at this breakpoint. However, the file dialog never shows up.
Through debugging, I found out that if I put an alert(...);
after the #choose-file-button click()
line, then the alert shows up and the normal page "Open File" dialog shows up (not my file dialog). If I do not have this alert, however, nothing shows up at all.
Is this a bug? How can I fix it and make my file dialog show up via the intercepted Ctrl+O
?
Edit: I just tested in Chrome, and it works perfectly. However, it still does not work in Firefox.