0

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>
3
  • @CertainPerformance Okay, I added code. Thanks. Commented Feb 28, 2019 at 6:43
  • ESC will not work here as you trigger ti in your thread but the file dialog is browser's part and is out of your sandbox. Commented Feb 28, 2019 at 6:46
  • On second thought, you cannot. File dialog gives access to file storage. JS being in sandbox does not have access to and should not even have. So to answer your question, no. there is no way to do that Commented Feb 28, 2019 at 6:57

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

8 Comments

Though it answers the question, not sure if it should be a comment or answer. Holding my vote as not sure
I have tried this. I AM sure. I had to run a Java service and listen on a socket, then trigger a native click to close a popup triggered by the browser once.
I completely agree that this is not possible via just JS. Just unsure that should we answer it or just comment it as it falls in that gray zone
Since it answers the question, it should be posted as an answer
The server can't send a keystroke on the client unless the server is running on the client.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.