I have a massive Javascript file capturing various keydowns on various screens of an application. I always use return false(); to prevent the default keydown action from occurring, which works great.
However, on this particular keydown, there is a confirm() box before the return false;. If I hit C, the confirm() dialogue comes up. If I move that out of the way, underneath it, the c has already been written to the text box.
How can I successfully suppress the keydown? This is only required to work in Chrome.
document.onkeydown = keyDown;
function keyDown()
{
switch(window.event.keyCode)
{
case(67):
if(confirm('You have unsaved data! Press OK to Save or Cancel to Discard')) {
alert('Do something...');
}
else {
return false;
}
window.location = '/invoice-12345';
return false;
break;
}
}
You can delay the confirm using
setTimeout.jsFiddle