• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

How to prevent refresh using Javascript?

Hi,
How do i prevent a user from refreshing (hitting F5 or reload button) on the browser, using a javascript?

PS: I have a javascript math game that i do not want the user to refresh, as it will reload/reset the whole game all over again.

0
tangteng78
Asked:
tangteng78
5 Solutions
 
Rajkumar GsSoftware EngineerCommented:
0
 
Michel PlungjanIT ExpertCommented:
My suggestion is to re-code your game so it can handle this.

A cookie or a hidden form field is a way to save state across a reload
0
 
Maverick_CoolCommented:
Add event handler for pageunload.
Prompt user if he want exit or refersh and warn him that game will close if he refreshes
if chooses not refersh, return false and cancel event bubble.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Michel PlungjanIT ExpertCommented:
M_C you mean
<body onbeforeunload="return 'you will restart the game'">
0
 
timhighamCommented:
" How do i prevent a user from refreshing (hitting F5 or reload button) on the browser, using a javascript "

You can't.

Javascript operates in what is know as a SANDBOX. The analogy is taken from similar to the explosives industry where for example a firework is placed in a box of sand. It can then operate to its maximum capability but will not affect anything out of the sandbox.
The brower and your PC are quite obviously placed outside the sandbox as other malicious code might infect it.
Having said that you can detect keystrokes with javascript, however you will not be able to stop a user clicking a browser button or menu item.

What is your intention of this game and how are your wanting to record results ? If you are monitoring in person you may want to have Javascript generate a unique serial code everytime the test loads to be placed against the subject's name before they start.

Consider that if you are not monitoring in person then you have no control over the client system and should look at additional setups such as server code requiring a particular user to login and then record when this user requests the test.
0
 
tangteng78Author Commented:
Thanks guys. Let me try all of the solutions above. Will get back to you guys. Appreciate the help.
0
 
timhighamCommented:
Just read this part of my post . . .

"  a unique serial code everytime the test loads to be placed against the subject's name  "

EG a timestamp as:

var timestamp=new Date().getTime();

To clarify this would be your own record system and nothing to do with Javascript coding, which is why I recommend server code to generate this if you wish fully automatic testing.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now