-1

I have the following body unload event listener

  window.onbeforeunload = () => {
     return "Changes you made may not be saved.";
  };

How do I remove that particular one when I'm done with the background processing I have to do?

Chris Hansen
  • 7,813
  • 15
  • 81
  • 165

1 Answers1

1

To remove an event listener(like you mentioned incase of multiple eventlisteners), you need to name your event handler function to an external function, not anonymous (so that you can reference that function):

Update 1:- I have added the message to be followed on screen

   

 window.addEventListener("beforeunload", customFunction);

    function customFunction(event){
        event.preventDefault();
        return event.returnValue = "Are you sure you want to exit?";
    }
    window.removeEventListener("beforeunload",customFunction);
Sunny
  • 235
  • 4
  • 17