JavaScript Open file
In this section, you will learn how to open and read the file in JavaScript.
You can see in the given example, we have created a file component to let the user select the file to be read and a button to submit the request. For this, we have used <input type="file">, through which we can browse the file and get the appropriate file to read. On clicking the "Open File" button, the function openFile() is called where we have used the JavaScript property window.location, which allows to access the selected file. The document.form.selectedFile.value display the text of the selected file on the browser.
Here is the code:
| <html> <h2>Read File in Javascript</h2> <script language="JavaScript"> function openFile() { window.location= 'file:///' + document.form.selectedFile.value; } </script> <body> <form name="form"> <input type="file" name="selectedFile"> <input type=button onClick="openFile()" value="Open File"> </form> </body> </html> |
Output will be displayed as:
Now click the browse button and select the file:

When you click the Open File button, you will get the text of the selected file:

Tutorials
- Clear cookie example
- JavaScript getElementById innerHTML
- JavaScript getElementById Style
- Javascript Examples
- JavaScript add row dynamically to table
- New Page 1
- JavaScript Change link
- JavaScript Checkbox getElementById
- javascript clear textarea
- JavaScript Clock Timer
- JavaScript Cookies
- JavaScript Date Difference
- JavaScript duplicate string
- JavaScript Email Validation
- javascript focus input
- JavaScript get excel file data
- JavaScript getAttribute Href
- JavaScript getAttribute Style
- JavaScript getElementById div
- JavaScript getElementById Iframe
- JavaScript getElementById select
- JavaScript Hide Button
- JavaScript Hide Div
- JavaScript hide image
- JavaScript Hide Table Column
- JavaScript Hide Table Rows
- JavaScript Key Event
- JavaScript link
- JavaScript method location
- JavaScript move div
- JavaScript move file
- JavaScript move image
- JavaScript Navigate Back
- JavaScript navigate to page
- JavaScript Navigate to URL
- JavaScript indexOf substring
- JavaScript onkeypress event
- JavaScript Open file
- JavaScript Open link in new window
- JavaScript Open Modal Window


