From 1b4b6f8a4b96b673ca1fdd4194e75da42f53d621 Mon Sep 17 00:00:00 2001 From: Roanna Victorio Date: Fri, 20 Oct 2023 22:53:44 -0700 Subject: [PATCH] adjust downloadFile() --- js/wallpaper.js | 68 +++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/js/wallpaper.js b/js/wallpaper.js index 1444721..09ed787 100644 --- a/js/wallpaper.js +++ b/js/wallpaper.js @@ -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); +}