Html file download button

i have a file that is a game made from HTML but I am making a website and I cant get a button when you click it it downloads the file to your computer because everytime I do it says file cannot be found if you have any questions about what I’m dong please ask because I need a lot of help

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

this is what I tryed

Download HTML File Download HTML File
<script>
    document.getElementById('downloadHtmlFileButton').addEventListener('click', function() {
        // Specify the HTML file URL here
        const fileUrl = 'dino.html'; // The uploaded file

        // Create an anchor element for downloading
        const anchor = document.createElement('a');
        anchor.href = fileUrl;
        anchor.download = 'dino.html'; // Specify the name for the downloaded file
        document.body.appendChild(anchor);
        anchor.click();
        document.body.removeChild(anchor);
    });
</script>

If you add a link to the page and set the download attribute, it will let the user download the page.

<a href="game.html" download>Get game</a>

If it is a game does it have JS and if so is it in a script element inside the HTML or in a separate script file?

2 Likes

can you show me an example

I don’t know what kind of example you need. You just add the download attribute to the page link.

The two pages/files must be inside the same root folder. The index page links to the game page and has the download attribute.

/index.html
/game.html

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Title</title>
  </head>
  <body>
    <a href="game.html">Play game</a>
    <a href="game.html" download>Download game</a>
  </body>
</html>
1 Like