-9

Current / code...

function myfunction() {
    alert("hello");
    document.getElementById('file').click();
}
<body onload="myfunction();">
    <input type="file" id="file" />
</body>

Now alert is displayed, but file is not clicked on onload event but if we change the event to onclick, the alert is shown as well as file is also clicked.

Eric
  • 6,563
  • 5
  • 42
  • 66
Neeraj Sharma
  • 297
  • 1
  • 4
  • 16

1 Answers1

1

Based on the security model of the modern browsers this is not possible. The click function will not work until the user has interacted with the page. The best I could come up with is the onfocus event which requires the user to do very little to interact with the page.

Try this...

function myfunction() {
    alert("hello");
    document.getElementById('file').click();
}
<body onfocus="myfunction();">
    <input type="file" id="file" />
</body>
Eric
  • 6,563
  • 5
  • 42
  • 66