16

When trying to invoke a .click() of an anchor tag to auto click the url. The code is working fine in all browsers except Internet Explorer v11.

Any help will be appreciated.

var strContent = "a,b,c\n1,2,3\n";
var HTML_APS = strContent;
var data = new Blob([HTML_APS]);
var temp_link = document.createElement('a');
temp_link.href = URL.createObjectURL(data);
temp_link.download = "report_html.htm";
temp_link.type = "text/html";
temp_link.style = "display:none";
document.body.appendChild(temp_link);
if (confirm("Press a button!") == true) {
  temp_link.click();
  temp_link.remove();
}

here is the fiddle.

Barmar
  • 741,623
  • 53
  • 500
  • 612
skoley
  • 311
  • 2
  • 7

3 Answers3

40

For IE, you can use navigator.msSaveOrOpenBlob

so, cross browser, the code would be

var strContent = "a,b,c\n1,2,3\n";
var HTML_APS = strContent;
var data = new Blob([HTML_APS]);

if (confirm("Press a button!") == true) {
  if (navigator.msSaveOrOpenBlob) {
    navigator.msSaveOrOpenBlob(data, "report_html.htm");
  } else {
    var temp_link = document.createElement('a');
    temp_link.href = URL.createObjectURL(data);
    temp_link.download = "report_html.htm";
    temp_link.type = "text/html";
    document.body.appendChild(temp_link);
    temp_link.click();
    temp_link.remove();
  }
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • so for IE "var data = new Blob([HTML_APS]);" this will not work instead This code "navigator.msSaveOrOpenBlob(data, "report_html.htm")" should be implemented ? – skoley Sep 15 '17 at 06:45
  • 1
    look at the code ... `var data = new Blob([HTML_APS]);` is for ALL browsers ... it's only HOW you download it that is different – Jaromanda X Sep 15 '17 at 06:46
1

When used download attribute an anchor, this signifies that the browser should download the resource the anchor points to rather than navigate to it.
It doesn't support IE11. For reference click here

Super User
  • 9,448
  • 3
  • 31
  • 47
1

Per this SO answer, the 'download' attribute has not been implemented in Internet Explorer.

The download attribute is not implemented in Internet Explorer.

http://caniuse.com/download

For Internet explorer you can use the "SaveAs" command.

jdgregson
  • 1,457
  • 17
  • 39