Skip to content
Advertisement

html download attribute redirects to url instead of downloading

Noob webdeveloper here.

I’m trying to download an image from an url on click. But when I use the image url as my href it just redirects to that url instead of downloading. Of course I am using the download attribute. I have tried my own code and also mulitple code blocks from other people. But all of them just redirect. I am using google chrome.

My code:

JavaScript

Code I used from someone else 1(accepted answer):

JavaScript

Code I used from someone else 2:

JavaScript

Some help will be appreciated. I might just be a big dum dum and overlook something extremely obvious.

Advertisement

Answer

The problem is because you’re using a cross-domain URL. From the documentation for the download attribute:

download only works for same-origin URLs, or the blob: and data: schemes.

To fix this you need to host the image on the same domain as the parent site.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement