r/learnjavascript • u/Low_Mammoth_9371 • Jan 19 '23
How to start a client-side download with JavaScript
22
u/MXMLNDML_ Jan 19 '23
You don’t need to add the <a> to the DOM
13
u/mynamesleon Jan 19 '23
Depending on your browser support, you may need to. It was needed for older versions of Chromium, but that's changed now (at least for the most part - some Chromium based browsers may still require this).
The problem is that some browsers won't recognise programmatically triggered events on elements that don't actually exist in the DOM - sometimes this is a setting, and sometimes it's a consequence of security settings.
6
u/LogicallyCross Jan 19 '23 edited Jan 20 '23
Does this start a download or just throw a dialog asking if you want to download something?
A native browser dialog I mean.
-4
u/main-medina Jan 20 '23
It starts a download. Specifically, the a.click is the event that starts the download
4
u/LogicallyCross Jan 20 '23
So you can force a download of anything on anyone? Doesn’t seem right.
4
5
Jan 19 '23
Is it a stylistic choice to add the comments below the line of code instead of above?
20
u/inrinsistent Jan 19 '23
I have personally never seen comments put below the line of code except for when it’s this person posting
3
2
3
u/Alternative_Crazy322 Jan 19 '23
What VS Code theme is this I need it
4
u/kap89 Jan 19 '23
It's Seti theme on https://carbon.now.sh - it is also available for VS Code, but might look a bit different.
1
1
7
u/Low_Mammoth_9371 Jan 19 '23
Code to copy: