首页 > 解决方案 > 在 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
    }

标签: javascriptfirefox

解决方案


我想到了。现代的便利使我能够避免没有充分地将新元素“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
    }

推荐阅读