Solved

How to prevent refresh using Javascript?

Posted on 2011-03-20
7
396 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
7 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 100 total points
Comment Utility
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 100 total points
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 75

Expert Comment

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

Assisted Solution

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now