392
Views
14
Comments
Solved
Disbale F5 and back button of browser
Question

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.



2021-04-09 11-42-43
assif_tiger
 
MVP
Solution

Hi Bhakti Shinde

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

Block back button:

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

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 != 'https://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);
});
2021-04-09 11-42-43
assif_tiger
 
MVP

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 != 'https://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;
  }
};
UserImage.jpg
Bhakti Shinde

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 != 'https://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.


UserImage.jpg
Pratham. Polekar

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);});


UserImage.jpg
Bhakti Shinde

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.


UserImage.jpg
Pratham. Polekar

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.


Disable backbutton.png
UserImage.jpg
Bhakti Shinde

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.

UserImage.jpg
Bhakti Shinde

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?


2020-03-24 00-01-07
Nuno Gonçalo Pereira

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




UserImage.jpg
Bhakti Shinde

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.


2020-03-24 00-01-07
Nuno Gonçalo Pereira
Are you in this menu web block?


Thanks and Best Regards,

Nuno Pereira

UserImage.jpg
Bhakti Shinde

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.

2014-10-21 20-15-17
Alberto Ferreira

Bhakti you must open healthCalculator eSpace and do it there

Regards

2021-04-09 11-42-43
assif_tiger
 
MVP
Solution

Hi Bhakti Shinde

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

Block back button:

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

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 != 'https://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);
});
2021-04-09 11-42-43
assif_tiger
 
MVP

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 != 'https://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;
  }
};
UserImage.jpg
Bhakti Shinde

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 != 'https://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.


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.