2

I want to write a script that downloads some audio files from a web page. Problem is, that simulating the "click" via element.click() doesn't work well, because the browser simply navigates to the URL of that anchor element. For example:

<a href="https://www.somesite.com/someAudioFile.mp3"></a>

const link = document.querySelector('a');

link.click();//Navigates to the url...

What I need, is to be able to collect all those links that I need, and simply trigger the download, without the save-as dialogue appearing

Is it possible? If so, how?

i.brod
  • 3,993
  • 11
  • 38
  • 74
  • Does this answer your question? [How can I create download link in HTML?](https://stackoverflow.com/questions/2793751/how-can-i-create-download-link-in-html) (the second answer) – Drdilyor May 30 '21 at 11:09

2 Answers2

1

You can add download attribute to element, without javascript

<a href="https://www.somesite.com/someAudioFile.mp3" download></a>

Or download with custom name

<a href="https://www.somesite.com/someAudioFile.mp3" download="music"></a>
Diyorbek
  • 44
  • 8
0
link.addEventListener('click', (e) => e.preventDefault());