0

In my program, the user is interrupted after a certain time with a JavaScript game (snake game). The popup window (snake game) will close by itself when the user finishes. That code will be in the game.

I need the popup to be on top until the user finishes the game. Here, the user is able to avoid the popup (by clicking anywhere outside of the popup) and go back to the parent webpage. I need the user to keep playing the game until he/she finishes it. Is there a way I can do this?

Please only include suggestions with JavaScript.

<html>
<head>
<title>test</title>
<script>
function popup(){
      var snakeGame
      setInterval(function(){
        snakeGame = window.open('snake.html', "_blank", "toolbar=0, titlebar=0, location=0, scrollbars=0, statusbar=0, status =yes, menubar=0, top=500, left=500, width=550, height=380").focus();
      },5000);
    }
</script>
</head>

<body>
  <p>Web page content</p>
  <body onload="popup()">
</body>
</html>
Tom Zych
  • 13,329
  • 9
  • 36
  • 53
ceylonese92
  • 93
  • 2
  • 10

3 Answers3

3

I would suggest creating a CSS popup that does not go away until the user finishes the game. (Creating a non-closeable popup window is impossible.)

You can visit https://stackoverflow.com/a/19065024/2930268 to see an example of a CSS Lightbox.

You would just have to make the box close by itself when the user finishes the game by doing document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'.

I hope this helps!

Community
  • 1
  • 1
416E64726577
  • 2,214
  • 2
  • 23
  • 47
  • This is unrelated to this answer, but could you please stop making edits that fix the spelling of something? They don't really improve the post. – tbodt Jul 16 '14 at 20:49
1

Look at using an iframe lightbox rather than opening a new window. That way both appear in the same browser window.

Most pre-written lightbox scripts should have options to not close until you call a function.

Demo of a lightbox : http://digitarald.de/project/squeezebox/1-1/showcase/iframed/ - this one runs with mootools, but there are plenty for other libraries (and presumably stand-alone javascript ones)

(Also, note that client-side code is always cheatable if someone knows what they are doing)

Also, with your current system, you could always run a script when the parent and child window are focussed on. If someone clicks on the parent one without winning the game, you could display a message that they must go back to the game.

This should give you the info needed to get such variables:

Is there a way to detect if a browser window is not currently active?

Community
  • 1
  • 1
RichardB
  • 2,615
  • 1
  • 11
  • 14
0

This will place the window on top of all other windows until the user closes it (or in your case finishes the game).

<body onblur="self.focus();">

But do keep in mind that a few tricks by the user (they have to be fairly knowledgeable) can disable this "always on top" functionality.

Edit: For what you're trying to do, a CSS Lightbox might be a better solution, but if you're sticking to windows, this should help.