Tuesday, April 6, 2010

Disable Refresh in Webpage Using Javascript (F5 & Ctrl + R)

Okay here's another script for disabling refresh (F5 & Ctrl + R) in your web pages.


document.onkeydown = function() {    
    switch (event.keyCode) { 
        case 116 : //F5 button
            event.returnValue = false;
            event.keyCode = 0;
            return false
        case 82 : //R button
            if (event.ctrlKey) { 
                event.returnValue = false
                event.keyCode = 0;  
                return false
            } 
    }
}

Same as before, you can add this to your main javascript file and place it in the head part of your page.

It works by adding a function to your page's onkeydown event. Then we simply get which button was pressed by using event.Keycode (116 for F5 and 82 for R). Also, notice the "if" statement for R, it simply checks if the control button was pressed at the same time as R by checking event.ctrlKey

But there's a limitation to handling page refreshes with this code. You simply cannot control the refresh button specific to your browsers' toolbar. 



So the only way to really prevent users to refresh the page is by using this code and at the same time opening your page at a new window without the toolbar. 

Check http://codingresource.blogspot.com/2010/01/javascript-windowopen-in-net.html for more info on how to do that.

16 comments:

  1. Hi,

    document.onkeydown = function() {
    ...
    }

    works correct with IE but not with FF.

    Greetings
    Steffen

    ReplyDelete
    Replies
    1. yes . its only working with IE .

      Delete
  2. hey it really worked for me. Thank you.

    ReplyDelete
  3. it's really working..thanks..

    ReplyDelete
  4. Hi!

    I'm new in this, just wonder it is work on ASP page? I'm using ASP not ASP.net.

    Please let me know where is the following code to be place?

    document.onkeydown = function() {
    switch (event.keyCode) {
    case 116 : //F5 button
    event.returnValue = false;
    event.keyCode = 0;
    return false;
    case 82 : //R button
    if (event.ctrlKey) {
    event.returnValue = false;
    event.keyCode = 0;
    return false;
    }
    }
    }

    Thanks!

    Regards,
    Michael

    ReplyDelete
  5. works fine in chrome...
    the code is not working in firefox...........any solution????

    ReplyDelete
  6. document.onkeydown = function(e) {
    var key;
    if (window.event) {
    key = event.keyCode
    }
    else {
    var unicode = e.keyCode ? e.keyCode : e.charCode
    key = unicode
    }
    switch (key) {//event.keyCode
    case 116: //F5 button
    alert("11");
    event.returnValue = false;
    key = 0; //event.keyCode = 0;
    return false;
    case 82: //R button
    if (event.ctrlKey) {
    alert("11");
    event.returnValue = false;
    key = 0; //event.keyCode = 0;
    return false;
    }
    }
    }

    for IE,FF,Chrome. It's work

    ReplyDelete
    Replies
    1. If you remove alert("11") from the above, it is not working for both IE and FF.

      Delete
  7. document.onkeydown = function(e) {
    var key;
    if (window.event) {
    key = event.keyCode
    }
    else {
    var unicode = e.keyCode ? e.keyCode : e.charCode
    key = unicode
    }

    switch (key) {//event.keyCode
    case 116: //F5 button
    event.returnValue = false;
    key = 0; //event.keyCode = 0;
    return false;
    case 82: //R button
    if (event.ctrlKey) {
    event.returnValue = false;
    key = 0; //event.keyCode = 0;
    return false;
    }
    case 91: // ctrl + R Button
    event.returnValue= false;
    key=0;
    return false;
    }
    }

    ReplyDelete
  8. Thanks For posting but i need exact key when i pressed ctr+r i.e for ctrl the key is 17

    ReplyDelete
  9. function preventCommand(e) {
    var keyCode = (e.keyCode) ? e.keyCode : e.which;
    var isCtrl;


    if(window.event)
    isCtrl = e.ctrlKey
    else
    isCtrl = (window.Event) ? ((e.modifiers & Event.CTRL_MASK) == Event.CTRL_MASK) : false;


    if(isCtrl) {
    if(String.fromCharCode(keyCode).toLowerCase() == 'r') {
    alert('They pressed Ctrl+R');
    return false;
    }
    }


    if(keyCode == '116') {
    alert('They pressed F5, but you can not prevent the refresh :P');
    return false;
    }


    return true;
    }

    I got the solution but when am using firefox it's not working my intention is when ever i pressed ctrl+r i have to run some functionality in my application please any one try to help me it's my humble request

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Dear all,

    This code is for stop the functionality of Refresh button


    I have found the problem of existing code and i resolved all issues in that. pass the event attribute in function call it where you want. it is working fine in all browsers like Chrome, Mozilla Firefox, IE all versions, Opera , Safari etc.

    Just copy and paste this code into your java script file and call that file where you want



    ===========================================

    // F5 button and cntrl+r (reload) disabled
    document.onkeydown = function(event)
    {
    switch (event.keyCode) {
    case 116: //F5 button
    event.returnValue = false;
    event.keyCode = 0;
    return false;
    case 82: //R button
    if (event.ctrlKey) {
    event.returnValue = false;
    event.keyCode = 0;
    return false;
    }
    }
    }



    ==========================================


    If any Queries feel free and contact at : venkat0553@gmail.com

    ramireddynec.blogspot.com



    Regards,

    B.V.Rami Reddy,

    Senior Software Developer

    ReplyDelete
  12. this code will work perfectly.. dont use event.returnValue..
    its kei.returnValue=false;
    then it will work good

    document.onkeydown = function(e) {
    var key;
    if (window.event) {
    key = event.keyCode
    }
    else {
    var unicode = e.keyCode ? e.keyCode : e.charCode
    key = unicode
    }

    switch (key) {//event.keyCode
    case 116: //F5 button
    key.returnValue = false;
    key = 0; //event.keyCode = 0;
    return false;
    case 82: //R button
    if (event.ctrlKey) {
    key.returnValue = false;
    key = 0; //event.keyCode = 0;
    return false;
    }
    case 91: // ctrl + R Button
    event.returnValue= false;
    key=0;
    return false;
    }
    }

    ReplyDelete
  13. To refresh page in Safari, we need to use "cmd+r". but it's not working.

    ReplyDelete