javascript - 在 Firefox 26 中下载 webform JSON 对象列表
问题描述
我被迫使用旧的 Firefox 26 版。我知道下面的代码可以在 Firefox 72 上完美运行,我知道。在逐步执行下载功能时,我意识到我的问题与 a.click() 有关。在比较浏览器之间的调试控制台时,我没有注意到函数有任何差异,但是 a.click() 没有触发 saveAs 弹出窗口。
这是 json 列表格式,其索引等于 n 个选择框元素。
testn:{
val: "pass",
desc: "test description"}
JS
jsonData = $("#myform").serializeArray();
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
if (confirm("Save results to <SCRIPT_PATH_LOCATION>")){
download(JSON.stringify(jsonData), 'webform.results.json', 'text/plain');
} else {
return false
}
解决方案
我想到了。现代的便利使我能够避免没有充分地将新元素“a”附加到网络表单上。
JS
jsonData = $("#myform").serializeArray();
function download(content, fileName, contentType) {
var a = document.createElement("a");
//Insert these
document.body.appendChild(a);
a.style = "display: none";
//Insert complete
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
if (confirm("Save results to <SCRIPT_PATH_LOCATION>")){
download(JSON.stringify(jsonData), 'webform.results.json', 'text/plain');
} else {
return false
}
推荐阅读
- python - 如何找到最小长度为 3 的所有可能连续且不重叠的子列表
- c++ - 没有默认构造函数的继承?
- django - 在Django中,按钮url被意外调用,url请求执行方式不同
- video - 如何正确制作视频循环?
- makefile - make 无法读取自己写入的文件
- laravel - 多个 findForPassport ?例如使用用户名和电话号码登录
- python - 获取未排序的特征值的问题
- json - 根据响应 JSON 检查用户输入的详细信息
- visual-studio-code - 将 Web 应用程序从 Visual Studio Code 部署到 Azure,但省略了数据文件夹
- bash - 我想在 bash(cmder)中找不到命令时调用我的脚本?