Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
adjust downloadFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
roannav committed Oct 21, 2023
1 parent a6395cb commit 1b4b6f8
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions js/wallpaper.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
// selecting the download button in the wallpapers page

const downloadButtons = document.getElementsByClassName("wallpaper-download");

for(let i = 0; i < downloadButtons.length; i++){
const button = downloadButtons[i]
const filename = button.name
button.addEventListener("click", () =>{downloadFile(filename)})

}
// downloadButtons.forEach(button => {
// const filename = button.name
// button.addEventListner("click", () =>{donwloadFile(filename)})
// });


// defining the downloadFile function
function downloadFile(fileName){
const filePath = "../img/wallpapers/" + fileName;

const downloadFileName = fileName;

const a = document.createElement("a");

a.href = filePath;

a.download = downloadFileName;

a.click()
console.log("downloading in progress.")
document.body.removeChild(a);

}
// selecting the download button in the wallpapers page

const downloadButtons = document.getElementsByClassName("wallpaper-download");

for (let i = 0; i < downloadButtons.length; i++) {
const button = downloadButtons[i];
const filename = button.name;
button.addEventListener("click", () => {
downloadFile(filename);
});
}

// defining the downloadFile function
function downloadFile(fileName) {
const filePath = "/img/wallpapers/" + fileName;

const downloadFileName = fileName;

const a = document.createElement("a");
a.style.display = "none";
a.href = filePath;
a.download = downloadFileName;

// add link to the DOM, so it can be clicked
document.body.appendChild(a);

a.click();
console.log("downloading in progress.");

// To make this work on Firefox we need to wait
// a little while before removing it.
setTimeout(() => {
document.body.removeChild(a);
}, 500);
}

0 comments on commit 1b4b6f8

Please sign in to comment.