简体   繁体   中英

How to click an input (type=file) on onload event

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.

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 theonfocus 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM