JKowalski Posted August 11, 2020 Share Posted August 11, 2020 (edited) Hello, what is the code to load the content of a file from my PC which contains JS code when I use the "try it" screen here, running on chrome, windows10? <script scr="?????/javascript"> Thank you. Edited August 11, 2020 by JKowalski Link to comment Share on other sites More sharing options...
Ingolme Posted August 12, 2020 Share Posted August 12, 2020 The file:/// protocol would be used for this, but browsers don't allow websites to load Javascript files from your filesystem for security reasons. Link to comment Share on other sites More sharing options...
JKowalski Posted August 12, 2020 Author Share Posted August 12, 2020 Ah, now I understand. Thank you very much! Link to comment Share on other sites More sharing options...
Makwana Prahlad Posted August 17, 2020 Share Posted August 17, 2020 At first, you need to use a input[type=file] to get a File. <input type=file id=file/> <div id=result></div> And then use FileReader to read file to target format, just like base64, text, buffer. const file = document.getElementById('file').files[0] const result = document.getElementById('result') const reader = new FileReader reader.addEventListener('load', () => { result.innerHTML = reader.result }) reader.readAsText(file, 'UTF-8') See: https://developer.mozilla.org/en-US/docs/Web/API/FileReader Link to comment Share on other sites More sharing options...
Ingolme Posted August 18, 2020 Share Posted August 18, 2020 15 hours ago, Makwana Prahlad said: At first, you need to use a input[type=file] to get a File. <input type=file id=file/> <div id=result></div> And then use FileReader to read file to target format, just like base64, text, buffer. const file = document.getElementById('file').files[0] const result = document.getElementById('result') const reader = new FileReader reader.addEventListener('load', () => { result.innerHTML = reader.result }) reader.readAsText(file, 'UTF-8') See: https://developer.mozilla.org/en-US/docs/Web/API/FileReader If you read the thread instead of just the title, the question was regarding running Javascript from a local file. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now