简体   繁体   中英

Dynamically created ahref click event not working in firefox or safari?

i am creating ahref tag dynamically using javascript to download csv file generated using javascript code. The following code is working fine in chrome but doesn't work in safari or firefox...

var a = document.createElement('a');
var blob = new Blob([str], {'type':'application\/octet-stream'});
a.href = window.URL.createObjectURL(blob);
a.download = 'export.csv';
a.click();

any help is greatly appreciated..

To make it work in Firefox, just insert the new element into the DOM ( apendChild , etc).

I don't think this will work in Safari:

  • <a download=""> isn't (properly) supported yet, AFAIK. Eg a.download = will not do what you want. You could still use setAttribute but Safari will still ignore it.
  • I don't think Safari does support downloading of blob:-URIs, anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM