2

I want to prevent users from pressing the F5 button because of running exam test in my project.

If I use a normal browser window, my code works fine, but now I want to use it in a popup windows - in the popup windows my code is not working. My code is:

document.onkeydown=function(e) {
    var event = window.event || e;
    if (event.keyCode == 116) {
        event.keyCode = 0;
        alert("This action is not allowed");
        return false;
    }
}
andrewsi
  • 10,807
  • 132
  • 35
  • 51
  • 2
    There are a number of other ways of refreshing the page than F5. I don't think you'll be able to supress all of them. – Spudley Aug 15 '13 at 13:31
  • 1
    You have to save state in your application and restore it after refreshing. There's no way to prevent user of refreshing your page(F5 in not only way to do this) – Alma Do Aug 15 '13 at 13:33
  • @Spudley i know about that but i want to disable only f5 and right click of mouse as per requirement,,, – NOfil Memon Aug 15 '13 at 13:34
  • I usually use ctrl+R to refresh. Or I can use the refresh icon in the browser toolbar. Then there's the possibility of the browser Dev Tools: I can open that and do anything (including changing your JS code). And we haven't even started on browser plugins (with GreaseMonkey installed, again I can override anything on your page). If a user wants to refresh the page, he will. There's not much you can do about it. Stopping him from doing it one way will only make him do it another way. If this is important, then you need more robust protection, and it needs to be server-side. – Spudley Aug 15 '13 at 13:44
  • @Spudley i disabled toolbar & icon in toolbar and there have many instructor in the exam hall so no one user uses any add-ons or extension so just give me the code for f5 and right click of mouse ,,, – NOfil Memon Aug 15 '13 at 13:49

3 Answers3

1

try this

myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.focus();
myWindow.onkeydown=function(e) {
    var event = window.event || e;
    if (event.keyCode == 116) {
        event.preventDefault();
        alert("This action is not allowed");
    }
}
Amith
  • 1,424
  • 1
  • 10
  • 22
  • if u consider this as answer please mark it as answer and vote up..thanks for knowing it that it helped – Amith Aug 15 '13 at 14:13
1
document.onkeydown = function(e){
    var event = (window.event || e);
    if(event.keyCode==116){
        event.preventDefault();
        alert("This action is not allowed");
    }
}

You don't need this part of code: event.keyCode = 0; - changing keyCode after key is pressed won't affect anything.

Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
Alex
  • 1,336
  • 10
  • 19
0

If what you are looking for is to supress refreshing of your web page then you should ask why you need this. If it is so as not to resend the data already sent then you should use a different php file for doing the proccessing of your data and a different to display them to your user. This way you can check whether anything is awry and redirect him accordingly.

If you for some specific reason trully need to prevent refreshing through F5 then what Cobra_Fast suggested e.preventDefault(); should do the trick.

idipous
  • 2,868
  • 3
  • 30
  • 45