I'm trying to do something weird with the web browser for a valid reason. The things I am trying to do and how I am doing it as follows.
Create a new blank window upon clicking some button.
var newWindow = window.open('', 'new window', spec, true);
Add a input element of type file in the new window.
newWindow.document.write('< input type="file" id="select-file" >');
Dispatch a click event to the input element above immediately.
newWindow.document.getElementById('select-file').dispatchEvent(new Event('click'));
All three steps are done in a function which is fired by clicking on the button mentioned in step 1. When the button is clicked, step 1 will surely create a new window and step 2 will insert the input element. However, step 3 would just pass by without any error. When I press the button again while the window is remained open, only then step 3 would work as I expect it to.
The only difference between the two cases is that step 1, 2 is gone through only when the window has to be created. Step 1, 2 will be ignored when the window is already opened and reused.
Therefore, I am assuming that there has to be something wrong with dispatching the mouse event. But I have no clue. Can someone elaborate on this?
The code I am trying to work on is similar to this example I made.
`http://jsfiddle.net/dansoonie/q8tzyx6z/`
I think it's not working because of some other JSfiddle issue, but take a look if my explanation is not understandable. Thanks in advance.