Reputation: 7628
I made a fiddle for your quick understood.
https://jsfiddle.net/bexoss/hdr5824a/7/
Is there a way to close file dialog using JavaScript?
I tried to send ESC
key but the dialog is not dimissed.
<input id="input_file" type="file" />
<div></div>
<script>
// jQuery lib included in somewhere
$(document).on('click', '#input_file', function(e){
$('div').append('File dialog is opened. <br/>')
setTimeout(function(){
var esc = $.Event("keydown", { keyCode: 27 });
$('div').append('Escape key sent.<br/>')
$("body").trigger(esc);
}, 2000)
})
</script>
Upvotes: 0
Views: 1911
Reputation: 29091
No. There is no way using Javascript due to security issues. This applies to all browsers and all versions.
If you want to trigger a key stroke, it would have to be native on the operating system, and not through the DOM.
This is also why the dialog box is different for the same browser on different OS. The dialog comes from the OS, via the internal API, not the browser.
This also applies to window.alert
and window.prompt
Upvotes: 3