Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document.hasFocus does not work in older Opera browsers and Opera Mini #367

Closed
pavle-goloskokovic opened this issue Sep 21, 2017 · 5 comments
Assignees
Labels
Milestone

Comments

@pavle-goloskokovic
Copy link
Contributor

pavle-goloskokovic commented Sep 21, 2017

  • A bug in the API:
    • Phaser version(s): 2.8.7
    • Live example:
      You can use Phaser CE game template, load it in any browser that does not support hasFocus
      (here is the table of browsers with their respected versions supporting this method https://www.w3schools.com/jsref/met_document_hasfocus.asp),
      run it and click anywhere on the canvas.
    • What should happen:
      Game should not break on mouse click.
    • What happens instead:
      Game breaks on mouse click and an error occurs:

    Unhandled Error: 'document.hasFocus' is not a function

This change
f8d4574#diff-3d1f84893e3059298765938e947d6639R266
now breaks any game played in a browser that does not support hasFocus method which includes older versions of Opera and Opera Mini.
It should be checked if this method is available, use it if it is, and find another way to check if document has focus for browsers where it is not available.
This stack overflow thread might have a solution for that:
https://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active

@samme samme added this to the 2.8.8 milestone Sep 21, 2017
@samme samme added the bug label Sep 21, 2017
@samme
Copy link
Collaborator

samme commented Sep 21, 2017

We do use the Page Visibility API as well.

f8d4574 is a fallback for when focus isn't triggered.

@pavle-goloskokovic
Copy link
Contributor Author

That is good but the issue is not if Page Visibility API is used or not.
The issue is that when document.hasFocus is used and it is not implemented an error is thrown as I mentioned above.
It doesn't actually break the game because it is thrown in an event handler so it does not affect execution of the main thread, just the logic inside of that handler. So

_this.visibilityChange(event);

is never executed on click if document.hasFocus is undefined.

@pavle-goloskokovic
Copy link
Contributor Author

Maybe a boolean isInFocus could be added to the
Game class (https://github.com/photonstorm/phaser-ce/blob/master/src/core/Game.js)
and it could be set to false in Game#focusLoss (https://github.com/photonstorm/phaser-ce/blob/master/src/core/Game.js#L1235)
and set to true in Game#focusGain (https://github.com/photonstorm/phaser-ce/blob/master/src/core/Game.js#L1253).
Then this.isInFocus could be checked instead of document.hasFocus() inside of _onClick event handler (https://github.com/photonstorm/phaser-ce/blob/master/src/core/Stage.js#L265)
to see if

_this.visibilityChange(event);

should be executed or not.

@samme
Copy link
Collaborator

samme commented Sep 21, 2017

I understand, I'm still going to fix the Opera error.

I just meant that f8d4574 was required to fix #236 as our use of the Page Visibility API doesn't cover that situation.

For now I'm just going to check for document.hasFocus support and omit the click handler if it's missing.

@pavle-goloskokovic
Copy link
Contributor Author

Fair enough.
Just to be clear in that case #236 will not be fixed for browsers that don't support document.hasFocus.
So if anyone is loading game in an iframe in any of those browsers game will not resume after loss and regain of focus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants