0

Within my Laravel project, I need to give a warning message, when browser close button click event or browser's tab button close event.

When only (ctrl+shift+R) refresh the browser, 'Changes you made may not be saved.' default message will display. Otherwise nothing.

<script type="text/javascript">

  window.addEventListener('beforeunload', function (e) 
  {
  // Cancel the event
  e.preventDefault();
  console.log(e);
  // Chrome requires returnValue to be set
  e.returnValue = 'Are you sure?';
  });


</script>

This warning return when reload the page '[Violation] Added non-passive event listener to a scroll-blocking 'touchstart' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952'

Nuwan Withanage
  • 393
  • 7
  • 19

1 Answers1

0

I've just tested this in Chrome (using DevTools) and it works, from another SO answer, here:

Detect browser or tab closing

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "\o/";

  (e || window.event).returnValue = confirmationMessage; //Gecko + IE
  return confirmationMessage;                            //Webkit, Safari, Chrome
});

If this isn't working, consider if your event listener is being registered.

JRK
  • 3,686
  • 1
  • 13
  • 19
  • Thankx for quick response @JRK. Same result also for the above code and it returns 'console.log(confirmationMessage)' as '0/' . How could I register the event listener? or How could I check whether is listener registered? – Nuwan Withanage Jun 11 '19 at 05:14