Skip to main content

How to Prevent Users to Press F5 key (Refresh button)

Some times we want to prevent user to Press F5 key to prevent Page Refresh.
You can use the below script. Paste this code in the Head Section of your Page.
<script language=javascript>
document.onkeydown = function(){
if(window.event && window.event .keyCode == 116){
window.event.keyCode = 505; // Capture and remap F5
}
if(window.event && window.event .keyCode == 505){
// New action for F5
return false; // Must return false
}}
</script>

Comments

Anonymous said…
this code is not work me please send me some other code. my problem is that i have create one web application and i put one timer when timer reach at zero it show the msg "Time Up!".

But my problem is that when ever i press refresh button or F5 key timer starts with begin time.

i want that when ever page is refreshed time will not change it continuous with same time...

please help me out.

please send a solution on my email id :

nitinvaghela.333.ce@gmail.com
Anuj Rathi said…
Dear Nitin,
When you press F5, your page loads again. Means all vaiables that you set using javascript also reset. So you can't do this through javascript.You will have to store your timer value to server side & you can do this through AJAX easily.

Regards,
Anuj Rathi
abell said…
interesting...
but i think you couldn't have done this without knowing the keycodes...
so, do you know a site or something or a way to know the code for every key?
abell said…
it's me again :P
i adapted your code to mine and it's awesome. very useful when checking security and insertion issues.
but there's still one way to refresh the page by right-clicking and hitting the option "refresh".
do you know some way of preventing this?
Anonymous said…
Hi Anuj,

Your script works fine, thanks.
However, if you press Ctrl+R the pages refreshes anyway.

Do you think you could modify your script to avoid this?

Thanks and regards,

Enrique
Anuj Rathi said…
Dear Abell,
If u wan to avoid Mouse Right Click, then u can use my another script by which u can avoide right mouse click. So no one will be able to refresh the page (using F5 or with right mouse click)

http://anujkrathi.blogspot.com/2007/06/how-to-prevent-right-mouse-click-in-ie.html

Regards,
Anuj Rathi
Anonymous said…
we can refresh the page in the browser using refresh button in the browser i.e. after address bar......how to avoid that??

Popular posts from this blog

Access Different interface method with same Name implemented in a class

We use interface and multiple interface inherit it into class, then we implement the methods of interface as it is necessary but what will happen if two interface have the same mentods with same signature and inherited in the same class? Now questions arises from here are : Q1) Both the methods will be implementable which is with the same name and signature? Q2) If methods are implementable then how it is possible? Q3) If it is possible then can we access both the methods? Q3) If it is possible then what is the way to access these methods publically? The answer of all the above question is below: 1) Yes both the methods with same name and signature is implementable. 2) we have to implement interface explicitely preceding with iterface name. 3) Yes it is possible to access the methods. 4) Yes we can

Should have on tips of our lips.........ACID PROPERTIES

Introduction    When ever we process any DML statements transactions are implicitly called. Ie. when ever no begin  transaction statement insert ,delete, update statements will have transaction statements around them implicitly. ACID concept is one of the oldest and important properties of the Database. It sets forward four goals that every database management system must strive to achieve: atomicity, consistency, isolation and durability. No database that fails to meet any of these four goals can be considered reliable. Atomicity Atomicity states that database modifications must follow an "all or nothing" rule. Each transaction is said to be "atomic." If one part of the transaction fails, the entire transaction fails. It is critical that the database management system maintain the atomic nature of transactions in spite of any DBMS, operating system or hardware failure. Consistency Consistency states that only valid data will be written to the database. If,...