L
L
lanvin lanvin072016-05-08 20:49:11
JavaScript
lanvin lanvin07, 2016-05-08 20:49:11

Why is the file not saved by getElementById?

This code

document.getElementsByTagName('a')[0].onclick = function() {                
  var csv = "A";
  var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
  this.href = csvData;
  this.target = '_blank';
  this.download = 'filename1.csv';
}
works, the file is saved, and this one
document.getElementById('btn').onclick = function() {                
  var csv = "BUTTON";
  var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
  this.href = csvData;
  this.target = '_blank';
  this.download = 'filename2.csv';
}
no.
Why?
FileSave

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Pautov, 2016-05-08
@lanvin07

Take a closer look at the code. The script simply takes and sets the link href with csvData value. After that, the browser, not understanding the format, downloads it. Why the hell does a button have a href attribute?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question