Search code examples
javascripthtmlflash

How can I create an html/javascript file to play swf file of my choice?


OK, so here's the rub. I downloaded a ton of SWF games online, and I want to create my own way to use them. I want to create a directory in the main menu of my HTML file that brings me to another menu with a ton of different choices of games. I know how to play them using the embed tag, but I don't want to create 40 different HTML files for each different game. I know there's a way to click any game you want and go to one HTML page, remembering which game you clicked, and play it, I just don't know what it is. I want every game to point to one embed.html page and it play the SWF file that you clicked. Any help would be much appreciated. I'm working on a work-around to play games on our crappy school Chromebooks. I might be overlooking something really simple, but I don't care. This would be amazing to have. Thanks StackOverflow!


Solution

  • Changing SRC of <embed> to change file being shown

    Here is something you can try. You can have a button for each game, name the button to the game. Then you can have an <embed> item with the src set to a default item. Then using the following JS: document.getElementById("game").src = "game.swf" to change the src of the item to whichever game you would like. Here is an example site I created with two random .swf files I downloaded off the the internet. There is a .swf file and when you click the button the .swf file is changed to a different file:

    <html>
    
    <head></head>
    
    <body>
        <button onclick="changeGame()">Click to change</button>
        <embed id="game" src="BannerSnack-ad-336x280.swf" type="">
        <script>
            function changeGame() {
                document.getElementById("game").src = "Car-speakers-590x90.swf"
            }
        </script>
    </body>
    
    </html>
    

    You can create a function for each button and game you want to use, this should be the simplest way to do this as I assume you are new to JS. This allows you to have one page with a number of buttons and one <embed> tag to hold the game.

    Edit

    For your problem it looks like using something like this might help:

    <button onclick="clicked()">click</button>
    
    <script>
        function clicked() {
            var opened = window.open("");
            opened.document.write("<html><head><title>Game</title></head><body><embed id=\"game\" src=\"game.swf\" type=\"\"></body></html>");
        }
    </script>
    

    This basically uses JS to create a new web page with an embed section. You can create a new function for each game like above. Hope this is what you were looking for.