-2

I want to stop refresh the page in browser

  • By using F5 key
  • By using Rightclick mouse button
  • By using Browser refresh button

Above first I got it in javascript but I need to disable the browser refresh button. I got it answer of first option by code.

function disableF5Btn(e) {
    if ((e.which || e.keyCode) == 116)
        e.preventDefault();
};

$(document).bind("keydown", disableF5Btn);

But now i am not able to do the 2nd and 3rd option.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Prabhat Singh
  • 192
  • 20
  • 2
    Firstly, you can't stop a user having full control of their browser, and secondly ***don't***. I would question why you need this behaviour, and suggest you find another way of doing what you require. – Rory McCrossan Jan 29 '15 at 11:31
  • 2
    More importantly *why* would you want to do this? – mbx-mbx Jan 29 '15 at 11:31
  • 2
    That is something you should not even try to do. You can not disable browser functionality, you can not turn off client computer and do similar stuff. Best practice here is to use `window.onbeforeunload` event and ask visitor if he is sure he wants to close/reload the page. [more...](http://stackoverflow.com/questions/1704533/intercept-page-exit-event) – skobaljic Jan 29 '15 at 11:32
  • If you want to prevent reloads due to some action going on in the background you may want to use 'beforeunload' event. – Artek Wisniewski Jan 29 '15 at 11:35
  • What are trying to do by preventing reload? –  Jan 29 '15 at 11:40

2 Answers2

0

You can prevent from right click like

$(document).bind('contextmenu', function (e) {
        e.preventDefault();
        alert('Right Click is not allowed');
});

Demo

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
0

You can do it by binding the contextmenu to the document,

$(document).bind('contextmenu', function (e) {
       alert('Right click is disabled');
       return false;
});

or

$(document).bind('contextmenu', function (e) {
           e.preventDefault();
           alert('Right click is disabled');
    });
sanjeev shetty
  • 438
  • 4
  • 17