logankilpatrick
logankilpatrick

Reputation: 14521

How to have a link trigger both a file download and take me to a new page?

I am trying to set up my website to download a file when a user clicks a specific link and then also take them to a separate page. From what I have gathered, this is not possible using pure HTML since we can't chain A tags together. Is this understanding correct? If so, how would I approach doing this in Javascript?

For context, I am trying to mimic the behavior on this site: https://code.visualstudio.com/download when you click the download VS Code button.

Upvotes: 1

Views: 559

Answers (1)

Matthews Masia
Matthews Masia

Reputation: 29

I can think of two ways to do this.

  1. You can handle all that in an event listener function for the download button to open a new location and also open a file link (to trigger download).

  2. You can handle it that an event listener opens a new link then triggers download with window.onload or window.contentLoaded.

Upvotes: 1

Related Questions