PDA

View Full Version : How to Block F5 Function key in all frames



yg32
07-25-2006, 03:09 AM
I want to block F5 key in a page which includes some frames. The following code can only block F5 in single page. How to modify this code to make it work to block F5 in all frames?

<SCRIPT LANGUAGE="JavaScript">
<!--
var asciiF5 = 116;
var bRet = true;
if(document.all){
document.onkeydown = onKeyPress;
}else if (document.layers || document.getElementById){
document.onkeypress = onKeyPress;
}

function onKeyPress(evt) {
window.status = '';
var oEvent = (window.event) ? window.event : evt;

var nKeyCode = oEvent.keyCode ? oEvent.keyCode :
oEvent.which ? oEvent.which :
void 0;
var bIsFunctionKey = false;
if(oEvent.charCode == null || oEvent.charCode == 0){
//alert(oEvent.keyCode);
bIsFunctionKey = (nKeyCode == asciiF5)
}
if(bIsFunctionKey){
bRet = false;
try{
oEvent.returnValue = false;
oEvent.cancelBubble = true;

if(document.all){ //IE
oEvent.keyCode = 0;
}else{ //NS
oEvent.preventDefault();
oEvent.stopPropagation();
}
window.status = msg;
}catch(ex){
alert(ex);
}
}
return bRet;
}
// -->
</SCRIPT>

jscheuer1
07-25-2006, 03:35 AM
Why? What's so bad about F5? In my current browser, it only reloads the page. Blocking that would be rather useless as there are many other ways to reload a page. Or, does F5 do something else in your browser?

yg32
07-25-2006, 04:01 AM
F5 is not bad. Our system is used in University. When some student keep on pressineg, the frequent reload of the web pages may make our database breaddown. So we have to block F5 in some pages.

jscheuer1
07-25-2006, 04:10 AM
So, you block F5. If someone is dumb/mean/bored enough to press it repeatedly, don't you think that they would then simply resort to hitting the refresh button repeatedly with their mouse?

yg32
07-25-2006, 04:15 AM
Maybe they don't mean to press F5, sometimes, they put their books or bags on the keyboard which will make our databse breakdown.

jscheuer1
07-25-2006, 07:28 AM
OK:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">

<html>

<head>

<title>Conforming HTML 4.01 Transitional Template</title>

<script type="text/javascript">

/* Block F5 refresh in frames John Davenport Scheuer
* As first seen in http://www.dynamicdrive.com/forums/
* Username: jscheuer1 - This credit must remain for legal use */

function nof5(){
if(window.frames&&window.frames[0]){
window.frames[0].focus();
for (var i_tem = 0; i_tem < window.frames.length; i_tem++)
if(document.all&&document.body.filters)
window.frames[i_tem].document.onkeydown=new Function("var e=window.frames["+i_tem+"].event; if(e.keyCode==116){e.keyCode=0;return false;};");
else if(document.getElementById)
window.frames[i_tem].onkeypress=new Function("e","if(e.keyCode==116){e.preventDefault();e.stopPropagation();}")
}
}
</script>
</head>

<frameset onload="nof5();" cols = "25%, 25%,*">
<frame src ="black.htm" />
<frame src ="black.htm" />
<frame src ="black.htm" />

<noframes>

<body>
<!-- place alternative information for accessibility purposes here -->
</body>
</noframes>

</frameset>
</html>

Notes: Only tested with blank pages inside the frames. This will only work if the page showing through the frame is from the same domain as the top page or frameset. It works for frames in IE 6, FF 1.5.0.4, and Opera 9 - perhaps others. It will work for iframes in IE 6 and Opera 9, not FF. To use it on pages containing iframes, add this to the end of the page, just before the closing </body> tag:



<script type="text/javascript">

/* Block F5 refresh in frames John Davenport Scheuer
* As first seen in http://www.dynamicdrive.com/forums/
* Username: jscheuer1 - This credit must remain for legal use */

if(window.frames&&window.frames[0]){
window.frames[0].focus();
for (var i_tem = 0; i_tem < window.frames.length; i_tem++)
if(document.all&&document.body.filters)
window.frames[i_tem].document.onkeydown=new Function("var e=window.frames["+i_tem+"].event; if(e.keyCode==116){e.keyCode=0;return false;};");
else if(document.getElementById)
window.frames[i_tem].onkeypress=new Function("e","if(e.keyCode==116){e.preventDefault();e.stopPropagation();}")
}
</script>

More Notes (added later): This can be used with FF for iframes but they must be initialized onload of their content. Example:


<iframe onload="nof5();" src="../black.htm" width="300" height="300" scrolling="" frameborder="1"></iframe>

and, in this case the full function as shown in the first code block in this post must be used. Fortunately, this will still allow the script to work for IE and Opera. Additionally, I found that your original script did not work in Opera, this version will and still works in IE and FF:


var asciiF5 = 116;
var bRet = true;
var msg='';
if(document.all&&document.body.filters){
document.onkeydown = onKeyPress;
}else if (document.layers || document.getElementById){
document.onkeypress = onKeyPress;
}

function onKeyPress(evt) {
window.status = '';
var oEvent = evt? evt : window.event;

var nKeyCode = oEvent.keyCode ? oEvent.keyCode :
oEvent.which ? oEvent.which :
void 0;
var bIsFunctionKey = false;
if(oEvent.charCode == null || oEvent.charCode == 0){
//alert(oEvent.keyCode);
bIsFunctionKey = (nKeyCode == asciiF5)
}
if(bIsFunctionKey){
bRet = false;
try{
oEvent.returnValue = false;
oEvent.cancelBubble = true;

if(document.all){ //IE
oEvent.keyCode = 0;
}else{ //NS
oEvent.preventDefault();
oEvent.stopPropagation();
}
window.status = msg;
}catch(ex){
alert(ex);
}
}
return bRet;
}

Both the frame script I wrote and the above can be used on the same page (only worth doing on a page with an iframe), if desired.

Twey
07-25-2006, 12:25 PM
I must say, I don't think much of your database. Have you tried MySQL (http://www.mysql.com/) or PostgreSQL (http://www.postgresql.org/)?

jscheuer1
07-25-2006, 03:11 PM
I must say, I don't think much of your database. Have you tried MySQL (http://www.mysql.com/) or PostgreSQL (http://www.postgresql.org/)?

I wasn't convinced at first that this was a good idea or needed but, what would happen to PostgreSQL and/or MySQL if they received repeated submissions?

mburt
07-26-2006, 06:12 PM
I agree with everyone here, but I have another reason for finding a way to block F5: if you have a page that uses frames, if you're a page that isn't thae homepage pressing F5 will just take you back to the index.htm (or whatever extension)

jscheuer1
07-26-2006, 08:07 PM
I agree with everyone here, but I have another reason for finding a way to block F5: if you have a page that uses frames, if you're a page that isn't thae homepage pressing F5 will just take you back to the index.htm (or whatever extension)

That might be what they want to do. They could still hit the refresh button.

Twey
07-26-2006, 09:40 PM
I wasn't convinced at first that this was a good idea or needed but, what would happen to PostgreSQL and/or MySQL if they received repeated submissions?They would queue the queries they couldn't handle, I imagine. I wasn't recommending them based on a specific feature, though (I can't compare them because I don't know what the OP is using originally), but no database server should crash due to repeated requests. If it uses up all the available memory, it should ignore new requests or drop the oldest to make room for the new. There's no reason to crash.

jscheuer1
07-27-2006, 04:15 AM
Maybe they don't mean to press F5, sometimes, they put their books or bags on the keyboard which will make our databse breakdown.


If it uses up all the available memory, it should ignore new requests or drop the oldest to make room for the new. There's no reason to crash.

So all in all, if we ascribe to this 'book bag theory', it could still be a bit of a mess, if not a crash. And depending upon the database/server setup and whether or not that is amenable to easy change/updating, perhaps a crash is a possibility. Anyways, I bought into this 'book bag theory' and came up with something that works. It can be tightened up but, since the OP seems to have lost interest in this thread, I'll just leave it as is for now. Oh, except for a little addition I've already thought up to test for window.frames[0] to avoid errors on pages not actually containing frames or iframes. I'll add that in with an edit to my previous post(s) in this thread.