Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript if statements prompting a download

Falke1up

New Coder
Hi!
Im kinda green in coding, but i got an idea for a project. I would really appreciate it if someone could tell me, if this is possible:

JavaScript:
If (condition === true) {
//download a specific file
}
else {
//download another file
}
 
Last edited by a moderator:
Solution
You want this behaviour for webpage or console? Anyway, here is simple example. I cant test now, but its atleast close to something that works.

JavaScript:
// function comapres is parameter true or false
// by that, one of two files is offered as download
function downloadFile(isFile1) {
  const file1Url = 'path/to/file1.ext'; // path to file 1
  const file2Url = 'path/to/file2.ext'; // path to file 2

  // compare which file is requested to be downloaded
  const downloadUrl = isFile1 ? file1Url : file2Url;

  // create anchor
  const a = document.createElement('a');
  a.href = downloadUrl;
  a.download = '';

  // append anchor to dom
  document.body.appendChild(a);

  // trigger the download
  a.click();

  // remove anchor from dom...
Hey!

You can probably use something like browser.downloads.download({url: "https://example.org/image.png"}) and have the link to your download. MDN has some very useful information, you can read more about working with files on their website Working with files - Mozilla | MDN.

But of course, if you need any help, let us know :)
 
You want this behaviour for webpage or console? Anyway, here is simple example. I cant test now, but its atleast close to something that works.

JavaScript:
// function comapres is parameter true or false
// by that, one of two files is offered as download
function downloadFile(isFile1) {
  const file1Url = 'path/to/file1.ext'; // path to file 1
  const file2Url = 'path/to/file2.ext'; // path to file 2

  // compare which file is requested to be downloaded
  const downloadUrl = isFile1 ? file1Url : file2Url;

  // create anchor
  const a = document.createElement('a');
  a.href = downloadUrl;
  a.download = '';

  // append anchor to dom
  document.body.appendChild(a);

  // trigger the download
  a.click();

  // remove anchor from dom
  document.body.removeChild(a);
}

// usage
downloadFile(true); // downloads file 1
downloadFile(false); // downloads file 2
 
Solution
You want this behaviour for webpage or console? Anyway, here is simple example. I cant test now, but its atleast close to something that works.

JavaScript:
// function comapres is parameter true or false
// by that, one of two files is offered as download
function downloadFile(isFile1) {
  const file1Url = 'path/to/file1.ext'; // path to file 1
  const file2Url = 'path/to/file2.ext'; // path to file 2

  // compare which file is requested to be downloaded
  const downloadUrl = isFile1 ? file1Url : file2Url;

  // create anchor
  const a = document.createElement('a');
  a.href = downloadUrl;
  a.download = '';

  // append anchor to dom
  document.body.appendChild(a);

  // trigger the download
  a.click();

  // remove anchor from dom
  document.body.removeChild(a);
}

// usage
downloadFile(true); // downloads file 1
downloadFile(false); // downloads file 2
Thank you, i wrote a version of this.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom