NOfil Memon
NOfil Memon

Reputation: 21

Prevent user pressing f5 button in pop up window

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;
    }
}

Upvotes: 2

Views: 2221

Answers (3)

Alex
Alex

Reputation: 1304

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.

Upvotes: 1

Amith
Amith

Reputation: 1434

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");
    }
}

Upvotes: 1

idipous
idipous

Reputation: 2910

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.

Upvotes: 0

Related Questions