I'm trying to dynamically upload and run a javascript file.
Here's my HTML code:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="file" id="myFile">
<script>
function fileUploaded() {
var myUploadedFile = document.getElementById("myFile").files[0];
var script = document.createElement('script');
script.type = "text/javascript";
script.src = myUploadedFile.name;
console.log("running the script: " + myUploadedFile.name);
document.body.appendChild(script);
}
document.getElementById("myFile").addEventListener("change", fileUploaded, false);
</script>
</body>
</html>
And I just upload a test.js
file with the following content:
console.log("Hello World!");
But when I upload the test.js
file, I just get the following message in the console:
running the script: test.js
And this is what I'm expecting to get:
running the script: test.js
Hello World!
How can I get the expected result?