0

How to detect user has click the Reload button on the browser:

enter image description here

I am trying to use the useBeforeunload library and javascript listener method beforeunload to detect however the event.preventDefault() and set the return value to false still doesn't do anything instead it keeps reloading the page even though I put some Modal tag to notify user that your going to reload the page and it did popup the modal right after I hit the Reload button but it force to reload the page. I also have issue in terms of detecting the Ctrl + Shift + R it seems it didn't detect for some reason even though I tried to capture it using keydown listener. What I wanted to achieve is to prevent it from reloading the page and show the modal to notify the user. Please see my code below:

App.jsx

  const [show, setShow] = useState(false);
  const onClose = () => setShow(false); // this will show the modal


  const disableF5 = ((e) => {
    if(window.location.pathname.includes('/apple/')) { 
      if(!config.enableRefresh) {
        if (e.which === 116 || e.keyCode === 116 || (e.key === "r" && e.ctrlKey) || (e.key === "r" && e.shiftKey && e.ctrlKey)) { // ctrl + shift + r never detected
          e.preventDefault();
          setShow(true);
          return;
        }
       }
    }
  });

  const handleReloadCancel = useBeforeunload(event => {
    event.preventDefault(); // this should prevent it from reloading the page
    setShow(true)
  });

  useEffect(() => {
    window.addEventListener('keydown', disableF5);
    window.addEventListener('beforeunload', handleReloadCancel);
    return () => {
      window.removeEventListener('keydown', disableF5);
      window.removeEventListener('beforeunload', handleReloadCancel);
    }
  }, [])

I also tried using this method window.performance.navigation.type == 1 but it didn't help as well. Any ideas on how to achieve my goal? Thanks!

Alvin Quezon
  • 1,089
  • 3
  • 11
  • 29
  • 4
    Why would to attempt to prevent someone from using their browser as they see fit? Does this help answer your question? https://stackoverflow.com/questions/68932621/put-a-warning-if-page-refresh-in-reactjs/68933242#68933242 – Drew Reese Nov 10 '21 at 01:38
  • Hi @DrewReese, we need to prevent it because this is a different page for transaction purpose (very sensitive information) and if they do so, the data being set is lost and they have to forced you to log you out from the application and try it again. So it should be that way. – Alvin Quezon Nov 10 '21 at 01:42
  • Hi @DrewReese, I think that popup (window.alert) message should be removed and also the it should not show the popup message and instead the modal should show and prevent it from reloading the page. Is this possible? – Alvin Quezon Nov 10 '21 at 01:59
  • 2
    Not really. Major browsers seem to be pretty hard against anything that blocks navigation actions. – Drew Reese Nov 10 '21 at 02:02

1 Answers1

-1

Regarding the detection of Ctrl + Shift + R:

Had the similar objective today and noticed that whenever the Shift key is among the pressed ones, then you have to check for the capital "R" letter.

Changing condition from (e.key === "r" && e.shiftKey && e.ctrlKey) to (e.key === "R" && e.shiftKey && e.ctrlKey) may do the trick for you as well.