简体   繁体   English

如何在特定文件的JavaScript中调用函数?

[英]How do I call a function in JavaScript on a specific file?

var handleFileSelect;
handleFileSelect=function(e){
    n=new FileReader;
    n.onload=function(e){
    localStorage.settings = e.target.result
    },
    n.readAsText(this.files[0])
},

document.getElementById("settings_file").addEventListener("change",handleFileSelect,!1);

So, for the code above, when a User uploads a .Json file, handleFileSelect is then activated on that file. 因此,对于上面的代码,当用户上载.Json文件时,然后在该文件上激活handleFileSelect。

I would like to change this so that there is no longer any need for the user to upload a file. 我想更改此设置,以便用户不再需要上传文件。 What I mean is that I would like handleFileSelect to run on a .Json file which I have in the same folder as this .js file. 我的意思是我希望handleFileSelect在.Json文件上运行,该文件与该.js文件位于同一文件夹中。

I feel like this is an easy thing to do, but I know no JavaScript whatsoever. 我觉得这很容易做到,但是我不知道任何JavaScript。

Thank you all for taking the time to read this. 谢谢大家花时间阅读本文。

I am coding this for a chrome extension that I am making. 我正在为此编写的Chrome扩展程序进行编码。

Edit: How could I call the handleFileSelect function on a certain string of characters? 编辑:如何在特定的字符串上调用handleFileSelect函数?

出于安全原因,除非用户明确为您提供文件,否则您将永远无法从用户的本地磁盘进行读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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