[RESOLVED]Change the behavior of the refresh button (F5) in chrome or firefox

HI ,

After I did some search I found this code which should do the job,but currently when I put it on the console and click run its not working,

what am I missing here ?

I want that when you click on the refresh button or F5 put and alert to the screen instead of refreshing the page.

Thanks!

    function disableF5(e) { if ((e.which || e.keyCode) == 116) e.preventDefault(); alert("clicked"};
    // To disable f5
        /* jQuery < 1.7 */
    $(document).bind("keydown", disableF5);
    /* OR jQuery >= 1.7 */
    $(document).on("keydown", disableF5);
    

You do have jQuery referenced on your page don’t you?

Thanks I forgot Embarassed

I’ve add it and now Im getting error the document is undefined ,any idea why?

Regards,

Miley

Try wrapping your event binding inside  document ready:

   function disableF5(e) { if ((e.which || e.keyCode) == 116) e.preventDefault(); alert("clicked"};
                                                                                        
$(function(){                                                                        
                                                                                        
    // To disable f5
        /* jQuery < 1.7 */
    $(document).bind("keydown", disableF5);
    /* OR jQuery >= 1.7 */
    $(document).on("keydown", disableF5);
       
});

By the way – you don’t need both of those binding lines in there.. As long as you’re pulling in the latest version of jQuery, you only need the  $(document).on("keydown", disableF5); line – not the   $(document).bind("keydown", disableF5); line… 

Leave a Reply