3

I am using Firefox ESR 45.2 and I'm using window.onbeforeunload to give the user a choice to leave the page or stay on it, when he leaves the window. This is my code:

window.onbeforeunload =  function  ( e )  {
  var e = e || window.event ;
  // For Firefox and IE
  if  ( e )  {
    e.ReturnValue =  'Any string' ;
  }
  // For Safari
  return  'Any string' ;
}

But the problem is that in Firefox, unless I open the developer tools once and then close it, the code does not work. I also looked at this question

Why does JavaScript only work after opening developer tools in IE once?

And I removed all console.logs. But still the window.onbeforeunload does not work. What could be the problem?

Community
  • 1
  • 1

1 Answers1

4

As per the documentation this was happening because firefox does not display prompts unless the page has been interacted with

https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

Thus is was not necessary to open the developer tools once, but some interaction with the page was necessary like clicking on the page.