0 votes
by (460 points)
Essentially, if the user refreshes the page I want to trigger a goto/display to a specific passage. I think this should be doable with window.onload or window.unload, but I have no idea what the syntax should be.

2 Answers

+1 vote
by (63.1k points)
selected by
 
Best answer

Something like this might work, though greyelf's idea is probably simpler/more foolproof. 

window.onbeforeunload = function () {
    window.sessionStorage.setItem('twine-reload-flag', 'true');
};

postdisplay['refresh-taker'] = function (t) {

    delete postdisplay[t];
    var refresh = sessionStorage.getItem('twine-reload-flag');

    if (refresh === 'true' && 
        passage() !== 'Start') { // your starting passage here 

        Engine.play('somePassage'); // passage to forward player to
    } 

};

This script might have some blind spots I'm not thinking of; all it does is set a bit of data in session storage when the page is unloaded.  It will ignore UI restarts and saving/loading, and because it's in session storage, the data won't persist or anything.

If you use this system, test it out in a couple of browsers; I only tested it in Chrome.

by (68.6k points)

A few observations about that code, as it's written while I write this:

  1. It will throw an exception any time Web Storage is disabled.
  2. You really should be using either jQuery's or the native event listening mechanisms, rather than the old on… properties.
  3. You could/should compare the current passage to Config.passages.start, rather than hard coding the starting passage's name.
by (63.1k points)
I'll write up a better version when I have time. That first one is definitely a problem, and number three is a very good point.

Question though, from a JavaScript noob: what's wrong with the .on... properties?
+1 vote
by (159k points)
edited by

The onload event occurs before the story format's engine loads so you cant use it to trigger engine functionality.

You could possibly set a Boolean variable (true) in your StoryInit special passage which you reset (false) in the startup passage of your story. You could then check if the variable has the StoryInit value in one of the later Task object events and then redirect to the goto passage if it has not been reset.

...