iiYO Community

Full Version: Action to redirect to another html page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there a way to redirect to another webpage at the end of the game?

Guest

Try putting this at the end of the
Code:
<body>
tag in the game's HTML page:

<pre>
Code:
<script>
game.interpreter.bus.subscribe(function () {
    location.href = "http://example.com/foo/bar/";
}, "wse.interpreter.end");
</script>
</pre>

Does this work?

Guest

I tried it, but I got this error in Chrome's debug console:
Uncaught ReferenceError: game is not defined

I tried putting that inside the function that starts the game and got:
Uncaught TypeError: Cannot call method 'subscribe' of undefined

It's no big deal really. I'll just end the last line of the dialogue with a link to the credits page.

Guest

There are basically two index.html files in use, one where the game object is global and one where it isn't. So you were right with putting the code into the function.

As for the second error: I guess the bus is not yet loaded at that point. Try it with
game.bus.subscribe(...
instead.

Guest

It works!!! Smile

Thank you!

Guest

Sorry to be a bother. I noticed that this code causes the page to redirect to the credits page, if the user clicks on the screen during loading of assets. any way to prevent that?
Here's what I'm using:
Code:
<script>
            (function ()
            {
                var game = new WSE.Game({
                    url: "game.xml",
                    host: typeof HOST === "undefined" ? false : HOST
                });
                WSE.extensions.sideImages.inject(game);
                if (location.href.match(/action=load/)) {
                  WSE.functions.savegames(game.interpreter);
                }
                game.bus.subscribe(function () {
                    location.href = "http://lalala.com/web/credits.html";
                }, "wse.interpreter.end");                
                game.start();
            }());
        </script>

Guest

I guess you could hook the subscribing to the end event to the assets.loading.finished event like this:

<pre>
Code:
<script>
            (function ()
            {
                var game = new WSE.Game({
                    url: "game.xml",
                    host: typeof HOST === "undefined" ? false : HOST
                });
                WSE.extensions.sideImages.inject(game);
                if (location.href.match(/action=load/)) {
                  WSE.functions.savegames(game.interpreter);
                }
                game.bus.subscribe(function () {
                    game.bus.subscribe(function () {
                        location.href = "http://example.com/web/credits.html";
                    }, "wse.interpreter.end");
                }, "wse.assets.loading.finished");                
                game.start();
            }());
        </script>
</pre>

Does it work?

Guest

Thank you! Works great! Big Grin