1

I am using Javascript with the window.onload and window.onbeforeunload functions.

In the onbeforeunload I want to run a code that removes an element from an existing list by iterating through the list via a method, but the method doesn't get executed.

When I use the same method in onload, everything works fine.

I think the code doesn't get executed in the onbeforeunload because the browser is already closed by then and that causes that the method doesn't get executed.

I think I need to do something Sychronous, do you guys have tips on how I can solve this?

// working
window.onload = function()
{
    MethodA();
};

// not working
window.onbeforeunload = function()
{
    MethodA();
};
Swag
  • 2,090
  • 9
  • 33
  • 63
  • 5
    What is the point of removing an item from the page right before the page closes? Please include the code for `MethodA()` if you want further help. – jfriend00 Apr 09 '14 at 17:59
  • Only way is to return something from `onbeforeunload`. Then the browser will alert the user and wait. – phylax Apr 09 '14 at 18:02
  • 2
    You need a return value in your `onbeforeunload` handler. See [this similar question](http://stackoverflow.com/questions/7255649/window-onbeforeunload-not-working) – John Apr 09 '14 at 18:02
  • @JohnRooney Lol... Return null did the trick! Wow! Thanks! – Swag Apr 09 '14 at 18:03
  • Please read about the [onbeforeunload](https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload) event – rafaelcastrocouto Apr 09 '14 at 18:03
  • @JohnRooney post it as a answer if you want, so I can accept it. – Swag Mar 23 '16 at 10:29

0 Answers0