Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How to Block F5 Function key in all frames

  1. #1
    Join Date
    Jul 2006
    Location
    UOW, Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile How to Block F5 Function key in all frames

    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>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,475
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2006
    Location
    UOW, Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,475
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2006
    Location
    UOW, Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Maybe they don't mean to press F5, sometimes, they put their books or bags on the keyboard which will make our databse breakdown.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,475
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK:

    Code:
    <!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:

    Code:
    <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:

    HTML Code:
    <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:

    Code:
    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.
    Last edited by jscheuer1; 07-27-2006 at 04:18 AM. Reason: add info & later to add additional error check
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I must say, I don't think much of your database. Have you tried MySQL or PostgreSQL?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,475
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey
    I must say, I don't think much of your database. Have you tried MySQL or PostgreSQL?
    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?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    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)
    - Mike

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,475
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by mburt
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •