0

I'm detecting window closed this way, and It works but I can't execute a function.

function myFunc(){ alert("byebye"); } window.onbeforeunload = function (event) { myFunc(); //Error, it doesn't execute return "Bye"; //It works, it shows an alert. }; 

How can I call myFunc when I'm closing a window/tab?

0

2 Answers 2

1

Try this by replacing alert by return.

function myFunc(){ return "byebye"; } window.onbeforeunload = function (event) { myFunc(); //Error, it doesn't execute return "Bye"; //It works, it shows an alert. }; 
Sign up to request clarification or add additional context in comments.

Comments

0

You can't call functions at the onbeforeunload event, you can only pass a string that is shown to the user.

3 Comments

Sorry - I was too fast - you can pass a function, but this function must return a string. See also developer.mozilla.org/de/docs/Web/API/WindowEventHandlers/…
What if I want a function to modify the UI instead of showing a message? I'm in trouble with that
Unfortunately you can't do anything else than showing a message when closing a window.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.