Online Shopping : Computers : Programming : Languages : JavaScript : Browser Settings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

199 of 286 people (70%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How Do I disable the Refresh button and keyboard F5 key???

Jan 21st, 2003 23:12
hong keong loo, nehal dave, Martin Arredondo,


this may help you
this will work in IE only
if (document.all){	 	
	document.onkeydown = function (){
	        var key_f5 = 116; // 116 = F5		
		if (key_f11==event.keyCode){
                            alert("F5 pressed");
				return false;
		}
	}
}
Found a solution that works in both IE and Netscape. Tested on IE6 and 
NS6.2
    function showDown(evt) {
        evt = (evt) ? evt : ((event) ? event : null);
        if (evt) {
            if (event.keyCode == 8 && (event.srcElement.type != "text" 
&& event.srcElement.type != "textarea" && event.srcElement.type !
= "password")) {
                // When backspace is pressed but not in form element
                cancelKey(evt);
            }
            else if (event.keyCode == 116) {
                // When F5 is pressed
                cancelKey(evt);
            }
            else if (event.ctrlKey && (event.keyCode == 78 || 
event.keyCode == 82)) {
                // When ctrl is pressed with R or N
                cancelKey(evt);
            }
        }
    }
    function cancelKey(evt) {
        if (evt.preventDefault) {
            evt.preventDefault();
            return false;
        }
        else {
            evt.keyCode = 0;
            evt.returnValue = false;
        }
    }
    // Additional code for NS
    if (navigator.appName=="Netscape") {
        document.addEventListener("keypress",showDown,true);
    }
    document.onkeydown  = showDown;