Tuesday, May 21, 2013

How to Block Refresh Key (F5) in JavaScript? - Source Code

Disable f5 and browser refresh using javascript

In this post I will show how to disable refresh key (F5) for a web page. It works on IE8, IE9, Mozilla14, Chrome26 browser as well. Find the source code below:-


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>How to Block Refresh Key (F5) in JavaScript?</title>

    <script type="text/javascript">

        var version = navigator.appVersion;

        function showKeyCode(e) {
            var keycode = (window.event) ? event.keyCode : e.keyCode;

            if ((version.indexOf('MSIE') != -1)) {
                if (keycode == 116) {
                    event.keyCode = 0;
                    event.returnValue = false;
                    return false;
            else {
                if (keycode == 116) {
                    return false;


<body onload="JavaScript:document.body.focus();" onkeydown="return showKeyCode(event)">
    <form id="form1" runat="server">
        Demo ..

Download source code click here


Item Reviewed: How to Block Refresh Key (F5) in JavaScript? - Source Code Rating: 5 Reviewed By: Ravi Kumar