Closed Bug 1091035 Opened 11 years ago Closed 1 year ago

click method on <a> that isn't in the document is a no-op (even when download attribute is specified)

Categories

(Core :: DOM: Events, defect, P5)

36 Branch
defect

Tracking

()

RESOLVED WORKSFORME

People

(Reporter: vicenzi.alexandre, Unassigned)

References

()

Details

(Keywords: testcase, Whiteboard: DUPEME)

User Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36 Steps to reproduce: Using this script: function exportToCSV(data, fileName, charset) { var buffer = data.join("\n"); var blob = new Blob([buffer], { "type": "text/csv; charset=" + (charset || "utf8") + ";" }); var link = document.createElement("a"); if (link.download !== undefined) { // HTML 5 link.setAttribute("href", window.URL.createObjectURL(blob)); link.setAttribute("type", blob.type); link.setAttribute("download", (fileName || "report.csv")); link.click(); } else if (navigator.msSaveBlob) { // IE 10+ navigator.msSaveBlob(blob, fileName); } else { // Non HTML 5 link.setAttribute("href", "data:" + blob.type + "," + encodeURIComponent(buffer)); link.click(); } } exportToCSV(["test", "other"], 'file.csv', 'utf8'); Actual results: Nothing. It works on Chrome and IE, but not on Firefox. Firefox 32 and Nightly 36. Expected results: Not sure, but I think start downloading the file.
Note that if you append the element, it works fine: http://jsbin.com/yonoyameze/1/edit?html,js,output
Status: UNCONFIRMED → NEW
Component: Untriaged → DOM: Events
Ever confirmed: true
Keywords: testcase
OS: Windows 8.1 → All
Product: Firefox → Core
Hardware: x86_64 → All
Summary: inline "a" data download not fired → click method on <a> that isn't in the document is a no-op (even when download attribute is specified)
Whiteboard: DUPEME
https://bugzilla.mozilla.org/show_bug.cgi?id=1472046 Move all DOM bugs that haven’t been updated in more than 3 years and has no one currently assigned to P5. If you have questions, please contact :mdaly.
Priority: -- → P5
Severity: normal → S3

Came here from https://stackoverflow.com/questions/35038884/download-file-from-bytes-in-javascript

I just tested this with Firefox 130.0 and the following code, and it works for me (i.e. the generated CSV file is downloaded):

<script>
function saveByteArray(reportName, byte) {
    var blob = new Blob([byte], {type: "application/csv"});
    var link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    var fileName = reportName;
    link.download = fileName;
    link.click();
};
</script>

<body>
<a href="javascript:saveByteArray('test.csv','abc')">Test</a>
</body>

This worked in 2019 already and older builds don't run on newer versions of macOS and I don't have access to my Windows machine right now, so running mozregression to find out when this got fixed is tough. Let's just close this out. Thanks for the update!

Status: NEW → RESOLVED
Closed: 1 year ago
Resolution: --- → WORKSFORME
You need to log in before you can comment on or make changes to this bug.