5

A similar question has been answered (Disable "Changes you made may not be saved" pop-up window), but somehow this still does not work for me.

I'm attempting to disable the Changes you made may not be saved. dialog box on form input changes. I know Chrome has removed the custom message capability, but I am having difficulty disabling the box altogether. I have the following javascript at the bottom of some HTML code:

  window.onbeforeunload = function() {
  };

  window.onbeforeunload = null;

Neither of the two work for me. When I type window.onbeforeunload into the inspector console, I get a null return value. Can the window.onbeforeunload = null; be called anywhere within the lifespan of the page? Perhaps I'm misplacing it somewhere?

Community
  • 1
  • 1
fibono
  • 773
  • 2
  • 10
  • 23
  • something could be adding an event listener the "right" way, i.e. `window.addEventListener('beforeunload', ...)` - but if it's your code, you'd know that already – Jaromanda X Dec 08 '16 at 22:09
  • 6
    if your page uses jQuery, you may be lucky and `$(window).off('beforeunload');` *may* work for you - as you've shared nothing about what libraries etc you use, this is a shot in the dark – Jaromanda X Dec 08 '16 at 22:12
  • @JaromandaX, that worked, thanks! Geez. I wonder why that happened? If you want to post an answer, I'll accept it. – fibono Dec 08 '16 at 22:14
  • before I add as an answer, it would be useful to know what JS libraries you are using (obviously jQuery is one), what others? It may be that some *helpful* jQuery plugin/addon is adding that *helpful*" event handler :p – Jaromanda X Dec 08 '16 at 22:16
  • You just have to use the opposite of whatever function you used to add the handler in the first place. If you use `.on()` you remove it with `.off`. If you used `.addEventListener` you use `.removeEventListener`. And if you used `window.onbeforeunload = function` you use `window.onbeforeunload = null` – Barmar Dec 08 '16 at 22:29
  • @JaromandaX, there are many libraries being used - jQuery, Semantic, Elemental, react are just a few. Sorry if that's not helpful.. – fibono Dec 08 '16 at 22:31
  • well, one of them is adding a `beforeunload` handler - I wont post as an answer, because it's not at all helpful without knowing what's doing it – Jaromanda X Dec 08 '16 at 22:32
  • Very late comment, but I had the same situation - with the window.onbeforeunload = null solution not working, and using jQuery .off() worked. – Andrew P. Jun 04 '20 at 07:45

1 Answers1

1

If you are using jQuery then below code will work for you

$(window).off('beforeunload');
Vijay Sasvadiya
  • 384
  • 1
  • 3
  • 7