Categories
JavaScript Answers

How to capture window.onbeforeunload with JavaScript?

Spread the love

Sometimes, we want to capture window.onbeforeunload with JavaScript.

In this article, we’ll look at how to capture window.onbeforeunload with JavaScript.

How to capture window.onbeforeunload with JavaScript?

To capture window.onbeforeunload with JavaScript, we return a value in the method.

For instance, we write

window.onbeforeunload = () => {
  return false;
};

to return false in window.onbeforeunload to show an alert box.

Conclusion

To capture window.onbeforeunload with JavaScript, we return a value in the method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *