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

How to prevent refresh using Javascript?

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.

5 Solutions
Rajkumar GsSoftware EngineerCommented:
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
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.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Michel PlungjanIT ExpertCommented:
M_C you mean
<body onbeforeunload="return 'you will restart the game'">
" 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.
tangteng78Author Commented:
Thanks guys. Let me try all of the solutions above. Will get back to you guys. Appreciate the help.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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