Solved

How to prevent refresh using Javascript?

Posted on 2011-03-20
7
426 Views
Last Modified: 2012-05-11
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
Comment
Question by:tangteng78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 100 total points
ID: 35175606
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 100 total points
ID: 35175707
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
 
LVL 7

Assisted Solution

by:Maverick_Cool
Maverick_Cool earned 100 total points
ID: 35176091
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35176256
M_C you mean
<body onbeforeunload="return 'you will restart the game'">
0
 
LVL 3

Assisted Solution

by:timhigham
timhigham earned 200 total points
ID: 35185936
" 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
 

Author Comment

by:tangteng78
ID: 35190289
Thanks guys. Let me try all of the solutions above. Will get back to you guys. Appreciate the help.
0
 
LVL 3

Assisted Solution

by:timhigham
timhigham earned 200 total points
ID: 35194540
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question