Share via


Prevent User from reloading a page

Question

Wednesday, May 2, 2018 11:53 AM

Hello,

Is it possible to restrict the ability to reload a web page (whenever the user presses F5 or Ctrl+R or clicks on reload page button) ?

Can i create a popup warning if the user tries to refresh or exit the web page?

Thank you.

All replies (6)

Wednesday, May 2, 2018 12:24 PM

if you add a popup on leaving a page, you will seriously irrate your users and go a along to looking unprofessional and looking like a scam site.

you can't stop someone refreshing the page


Wednesday, May 2, 2018 12:28 PM

It's an examination web application, i wanna inform the user that if they refresh or exit the page, the exam can be declined. That's why i need it.


Wednesday, May 2, 2018 12:30 PM

what is the page doesn't load properly, the css doesn't kick in, there are valid reasons for refreshing the page, why would an exam be declined on a refresh? and how are you going to monitor it?


Wednesday, May 2, 2018 12:47 PM

Hi,

Try perhaps https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload or https://stackoverflow.com/questions/2482059/disable-f5-and-browser-refresh-using-javascript but as pointed already does it bring a real benefit. The exam can or will be declined ? Make sure about the exact situation that you want to prevent.

Edit : or could it be that you are trying to prevent this because of some design flaw (ie you have 30 seconds to anwser each question but the timer starts again from 0 if the page is refreshed ???). It's easier to help when  understanding the final non technical need.


Wednesday, May 2, 2018 12:59 PM

hi

you can use below script for disable F5

 <script>
            window.onload = function () {
                document.onkeydown = function (e) {
                    return (e.which || e.keyCode) != 116;
                };
            }
       
     </script>

you just add the above code in your page


Wednesday, May 2, 2018 1:00 PM

I have LMS in which a module is for quiz, it was build in ASP.Net web forms but I have managed user and its test information, user have total time like 10 minutes and it is possible to navigate through questions , skip question also other options. In that case if user refresh page does not matter because server side timer control manage time.

In your case if you manage time on client side then then a user can open javascript console and do some actions that you don't want to allow him, you can use SessionStorage or LocalStorage of browser to save the test time and other information and on refresh you can retrieve time from it. Session storage is spscific to a session as compare to local storage.

My suggestion is that you need to manage time on server by using some state management technique which suits you like Server side session variable to maintain state.