Lock F5 key in the application

Asked

Viewed 267 times

2

Good afternoon, I am developing a Java Web application, and found that after performing a registration and typing F5 the information gets duplicated, only the ID that does not duplicate because it is serial. Can someone help me, if it is possible to block the F5. Thank you

  • hello, can be javascript?

  • 2

    As indicated by Maicon, there are several problems besides the F5 key (click refresh, enter in the address bar). The logic must be changed in addition to only locking the F5 key. Post your code so we can help more.

2 answers

0

One solution is to create a javascript function to prevent it from pressing the key corresponding to the F5

function desabilitaF5(e) { 
  if ((e.which || e.keyCode) == 116) 
    e.preventDefault(); 
};

$(document).on("keydown", desabilitaF5); // com jQuery
//document.addEventListener("keydown", desabilitaF5); // com JavaScript

The problem with that is that it can still go with the mouse and click on atualizar browser. To solve this you would have to create a mechanism to verify that the user who sent the request, is sending the same data as the last INSERT that you did and block.

0


Leticia, javascript version:

document.onkeydown = fkey;
document.onkeypress = fkey
document.onkeyup = fkey;

var foiPressionado = false;

function fkey(e){
        e = e || window.event;
       if( foiPressionado ) return; 

        if (e.keyCode == 116) {
             alert("F5 pressionado");
            foiPressionado = true;
        }else {
            alert("Proibido");
        }
 }

Demo

https://jsfiddle.net/felipe_douradinho/dv37ae95/2/embedded/result/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.