Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Catch user preventing to navigate away from tab

Discussion in 'WebGL' started by bigbadtrumpet125, Oct 30, 2015.

  1. bigbadtrumpet125

    bigbadtrumpet125

    Joined:
    Apr 1, 2013
    Posts:
    18
    In the WebPlayer build of our game, our web page would pop up a confirm dialogue when the user tried to navigate away from said page. We are trying to re-implement this in our WebGL code. Right now it appears that Unity overrides the window.onbeforeunload function (calls "OnApplicationQuit") which is what we were using for the WebPlayer. Our current work around is to override the window.onbeforeunlaod function after WebGL starts. I don't like this because it never fires the OnApplicationQuit functions in Unity. Is this the only workaround? Ideally I would like to leave the OnApplicationQuit functionality that Unity uses so we can receive this call as well if the user says they do want to navigate away from the page (if this is even possible).
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    I think this is something we should fix on our side, by calling any existing handlers in the one we set up. I have filed us a bug to fix it. Thanks.
     
  3. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    @bigbadtrumpet125 Looking at this in ore detail, it actually does not seem to be as easy as I first thought.

    1. If you override onbeforeunload to show a confirmation dialog, there is actually no way to get the result of that dialog. So all we could do is to always call "OnApplicationQuit", regardless of whether the player actually got quit or not.

    2. We have actually found that there is no deterministic behavior for any persistent operations (such as saving prefs or sending a WWW request) you might want to trigger in "OnApplicationQuit", on whether these will succeed or not. I see this as the only real use case for "OnApplicationQuit" - but when you cannot know if it will successfully complete or not, there is not a good point to have such functionality at all. So we have actually decided to remove the OnApplicationQuit handlers in WebGL, as they might leave you with the incorrect assumption that your data will be safe, when it is not.
     
  4. bigbadtrumpet125

    bigbadtrumpet125

    Joined:
    Apr 1, 2013
    Posts:
    18
    Thanks for the update, I looked more into our code base and it doesn't appear that we do much of anything on OnApplicationQuit that isn't related to editor stuff so that shouldn't be too big of an issue for us!