View Full Version : Help me to Block function keys and other keys across IE, NS

08-17-2005, 02:18 PM

I use this code to block function keys (F5) . It work but cursor focus on the file upload field it not work. Can you help me.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<title>--- F5 BLOCK ---</title>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
<script language="javascript">
var msg = 'That functionality is restricted.';
var asciiF5 = 116;
if(document.all){ //ie has to block in the key down
document.onkeydown = onKeyPress;
else if (document.layers || document.getElementById){ //NS and mozilla have to block in the key press;
document.onkeypress = onKeyPress;

function onKeyPress(evt) {
window.status = '';
//get the event object
var oEvent = (window.event) ? window.event : evt;
//hmmm in mozilla this is jacked, so i have to record these seperate what key was pressed
var nKeyCode = oEvent.keyCode ? oEvent.keyCode :oEvent.which ? oEvent.which : void 0;
var bIsFunctionKey = false;
/*hmmm in mozilla the keycode would contain a function key ONLY IF the charcode IS 0
else key code and charcode read funny, the charcode for 't'
returns 116, which is the same as the ascii for F5
SOOO,... to check if a the keycode is truly a function key,
ONLY check when the charcode is null OR 0, IE returns null, mozilla returns 0
bIsFunctionKey = asciiF5;
//convert the key to a character, makes for more readable code
var sChar = String.fromCharCode(nKeyCode).toUpperCase();
//get the active tag that has the focus on the page, and its tag type
var oTarget = (oEvent.target) ? oEvent.target : oEvent.srcElement;
var sTag = oTarget.tagName.toLowerCase();
var sTagType = oTarget.getAttribute("type");

var bRet = true; //assume true as that will be the case most times
if(sTagType != null){
sTagType = sTagType.toLowerCase();
if(bIsFunctionKey){ // Capture and stop these keys
bRet = false;

oEvent.returnValue = false;
oEvent.cancelBubble = true;
if(document.all){ //IE
oEvent.keyCode = 0;
}else{ //NS
window.status = msg;
return bRet;
Test Block Key F5
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="text" name="textfield">
<input type="checkbox" name="checkbox" value="checkbox">
<input name="radiobutton" type="radio" value="radiobutton">
<input type="reset" name="Reset" value="Reset">
<select name="select">
<option value="dsdsf">dsfds</option>
<option value="sdfds">sfdsfs</option>
<textarea name="textarea" cols="50"></textarea>
<input type="file" name="file">
<select name="select" size="5">
<option value="1">123456789</option>
<option value="2">12345678910</option>
<option value="3">12345678911</option>
<option value="4">12345678912</option>
<option value="5">12345678913</option>
<option value="6">12345678914</option>


reference site :

08-17-2005, 02:40 PM
This code is cross-browser compatible, therefore not running on a local hta application or somesuch. Why would you want to try to remove the user's shortcuts?

It's easy enough to remove the banned keypresses (in fact i have a script which stops sites from nulling my keypresses - so i'm not affected by disabling backspace to go one page back, i can still hit F5 to refresh, etc).

Also, users with JavaScript disabled will be unaffected by this code.

What's the code to be used for mate? There may be a more efficient solution available to you.