javascript - 获取文件到全局变量 Javascript
问题描述
有没有更好的方法来获取 .txt 文件,而不必将其写入隐藏段落然后从中读取?我想在全局变量中获取 .txt 文件的内容。
这就是我现在得到的:
async function loadFileAndPrintToConsole(url) {
try {
const response = await fetch(url);
const data = await response.text();
//console.log(data);
document.getElementById("debug1").innerHTML = data;
} catch (err) {
console.error(err);
setTestVariable(err);
}
getAlert();
}
function getVariable(){
txtVariable = document. getElementById("debug1").innerHTML;
}
这可以更简单地实现吗?
解决方案
而不是 document.getElementById("debug1").innerHTML = data; 你可以写 window.debug1 = data. 这就是我们设置全局变量的方式。
谢谢@pooria
推荐阅读
- mysql - 如何用sql搜索字母'å'
- python - 在已经存在的多对多中实现“通过”
- r-exams - 使用 mathml 转换器将粗体数学字符转换为 moodle
- reactjs - Gatsby:GraphQL 无法查询类型 \"Query\" 上的字段 \"allMarkdownRemark\"
- octave - 如何将 RGB 颜色分配给 Octave 中的散点图?
- java - 如何使用 HazelcastJsonValue 聚合?
- c++ - GCOV - 在不同文件夹中生成的 gcda 文件
- cron - 为什么我在 cron 中启动时 WebKit 不工作,但在终端中启动时工作?
- informatica - 如何将高级过滤表达式添加到 Informatica Data Integration Hub
- swift - 如何在 UIImage 上描边?