Disbale F5 and back button of browser

Hi,


I want to disable Refresh and back button of a browser.

Have used Javascript function -

SyntaxEditor Code Snippet

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

but it is not working.



Hi Bhakti,

you can use below javascript code to disable Refresh and back button of a browser.

 document.onkeydown = function (e) {  
            return (e.which || e.keyCode) != 116;  
        };  
history.pushState(null, document.title, location.href);window.addEventListener('popstate', function (event) {history.pushState(null, document.title, location.href);});


Pratham wrote:

Hi Bhakti,

you can use below javascript code to disable Refresh and back button of a browser.

 document.onkeydown = function (e) {  
            return (e.which || e.keyCode) != 116;  
        };  
history.pushState(null, document.title, location.href);window.addEventListener('popstate', function (event) {history.pushState(null, document.title, location.href);});

Hi Pratham,

Please find attached screen shot

I used the same in preparation action as below. But it is not working. When open in browser, it still is allowing to refresh the page.


Bhakti Shinde wrote:

Pratham wrote:

Hi Bhakti,

you can use below javascript code to disable Refresh and back button of a browser.

 document.onkeydown = function (e) {  
            return (e.which || e.keyCode) != 116;  
        };  
history.pushState(null, document.title, location.href);window.addEventListener('popstate', function (event) {history.pushState(null, document.title, location.href);});

Hi Pratham,

Please find attached screen shot

I used the same in preparation action as below. But it is not working. When open in browser, it still is allowing to refresh the page.


add javascript to the module.


Pratham wrote:

Bhakti Shinde wrote:

Pratham wrote:

Hi Bhakti,

you can use below javascript code to disable Refresh and back button of a browser.

 document.onkeydown = function (e) {  
            return (e.which || e.keyCode) != 116;  
        };  
history.pushState(null, document.title, location.href);window.addEventListener('popstate', function (event) {history.pushState(null, document.title, location.href);});

Hi Pratham,

Please find attached screen shot

I used the same in preparation action as below. But it is not working. When open in browser, it still is allowing to refresh the page.


add javascript to the module.


Hi Pratham,


I have added javascript to the module.

And when open in browser, clicked on Refresh.

Still it is allowing to refresh the page.

Pratham wrote:

Bhakti Shinde wrote:

Pratham wrote:

Hi Bhakti,

you can use below javascript code to disable Refresh and back button of a browser.

 document.onkeydown = function (e) {  
            return (e.which || e.keyCode) != 116;  
        };  
history.pushState(null, document.title, location.href);window.addEventListener('popstate', function (event) {history.pushState(null, document.title, location.href);});

Hi Pratham,

Please find attached screen shot

I used the same in preparation action as below. But it is not working. When open in browser, it still is allowing to refresh the page.


add javascript to the module.



Hi Pratham,


It is working fine for F5 button but when user clicks on the refresh or back option provided to the right of browser, it is allowing to refresh or back the page. How this option can be restricted?


Bhakti Shinde wrote:

Pratham wrote:

Bhakti Shinde wrote:

Pratham wrote:

Hi Bhakti,

you can use below javascript code to disable Refresh and back button of a browser.

 document.onkeydown = function (e) {  
            return (e.which || e.keyCode) != 116;  
        };  
history.pushState(null, document.title, location.href);window.addEventListener('popstate', function (event) {history.pushState(null, document.title, location.href);});

Hi Pratham,

Please find attached screen shot

I used the same in preparation action as below. But it is not working. When open in browser, it still is allowing to refresh the page.


add javascript to the module.



Hi Pratham,


It is working fine for F5 button but when user clicks on the refresh or back option provided to the right of browser, it is allowing to refresh or back the page. How this option can be restricted?


Hi!

Please, try to put this code inside of Menu JavaScript Webblock:

history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});




I hope it will helps you (works in Chrome, FF, IE, Edge).


Thanks and Best Regards,

Nuno Pereira




Nuno Gonçalo Pereira wrote:

Bhakti Shinde wrote:

Pratham wrote:

Bhakti Shinde wrote:

Pratham wrote:

Hi Bhakti,

you can use below javascript code to disable Refresh and back button of a browser.

 document.onkeydown = function (e) {  
            return (e.which || e.keyCode) != 116;  
        };  
history.pushState(null, document.title, location.href);window.addEventListener('popstate', function (event) {history.pushState(null, document.title, location.href);});

Hi Pratham,

Please find attached screen shot

I used the same in preparation action as below. But it is not working. When open in browser, it still is allowing to refresh the page.


add javascript to the module.



Hi Pratham,


It is working fine for F5 button but when user clicks on the refresh or back option provided to the right of browser, it is allowing to refresh or back the page. How this option can be restricted?


Hi!

Please, try to put this code inside of Menu JavaScript Webblock:

history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});




I hope it will helps you (works in Chrome, FF, IE, Edge).


Thanks and Best Regards,

Nuno Pereira




Hi Nuno Gonçalo Pereira,


I tried to put this code in Menu web block but it is not allowing to type inside menu web block.


Are you in this menu web block?


Thanks and Best Regards,

Nuno Pereira

Nuno Gonçalo Pereira wrote:

Are you in this menu web block?


Thanks and Best Regards,

Nuno Pereira


Hi Nuno Gonçalo Pereira,


Yes it is the same.

Bhakti you must open healthCalculator eSpace and do it there

Regards

Solution

Hi Bhakti Shinde

You can use the below JS to block the browser back button :

Block back button:

$().ready(function() {
    if(document.referrer != 'http://localhost:8181/'){ 
        history.pushState(null, null, 'login');
        window.addEventListener('popstate', function () {
            history.pushState(null, null, 'login');
        });
    }
});
Solution

assif_tiger wrote:

Hi Bhakti Shinde

You can use the below JS to block the browser back button :

Block back button:

$().ready(function() {
    if(document.referrer != 'http://localhost:8181/'){ 
        history.pushState(null, null, 'login');
        window.addEventListener('popstate', function () {
            history.pushState(null, null, 'login');
        });
    }
});


Hi assif_tiger,

It worked. Thank you !!!

Can you please help me with the code to disable Refresh?

I am using below code but it is not working properly.


SyntaxEditor Code Snippet

history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});

Bhakti Shinde wrote:

assif_tiger wrote:

Hi Bhakti Shinde

You can use the below JS to block the browser back button :

Block back button:

$().ready(function() {
    if(document.referrer != 'http://localhost:8181/'){ 
        history.pushState(null, null, 'login');
        window.addEventListener('popstate', function () {
            history.pushState(null, null, 'login');
        });
    }
});


Hi assif_tiger,

It worked. Thank you !!!

Can you please help me with the code to disable Refresh?

I am using below code but it is not working properly.


SyntaxEditor Code Snippet

history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});

Happy to know It works for you :)

Perhaps coming to Browser Refresh...

As of Breaking fundamental browser features is never a good idea !!!

Rather than blocking F5, just warn the user when they leave the page while the said process is happening. More often than not they're clicking the refresh button rather than pressing F5 anyway.

This will disable F5, but not the actual refresh function:

document.onkeydown = function (e) {
  if (e.keyCode === 116) {
    return false;
  }
};

assif_tiger wrote:

Bhakti Shinde wrote:

assif_tiger wrote:

Hi Bhakti Shinde

You can use the below JS to block the browser back button :

Block back button:

$().ready(function() {
    if(document.referrer != 'http://localhost:8181/'){ 
        history.pushState(null, null, 'login');
        window.addEventListener('popstate', function () {
            history.pushState(null, null, 'login');
        });
    }
});


Hi assif_tiger,

It worked. Thank you !!!

Can you please help me with the code to disable Refresh?

I am using below code but it is not working properly.


SyntaxEditor Code Snippet

history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});

Happy to know It works for you :)

Perhaps coming to Browser Refresh...

As of Breaking fundamental browser features is never a good idea !!!

Rather than blocking F5, just warn the user when they leave the page while the said process is happening. More often than not they're clicking the refresh button rather than pressing F5 anyway.

This will disable F5, but not the actual refresh function:

document.onkeydown = function (e) {
  if (e.keyCode === 116) {
    return false;
  }
};

Hi assif_tiger,

Thank you, It is working fine for F5 But as per my application requirement, wanna make it run for refresh button also.