How to Create a JavaScript Leave Page Warning?

Estimated read time 1 min read

Here’s an example of how you can create a leave page warning in JavaScript:

window.onbeforeunload = function() {
  return "Are you sure you want to leave?";
};

This code creates a function that will be triggered when the user tries to leave the page. The function returns a string that will be displayed as a warning message to the user.

Please note that due to security and user experience reasons, modern browsers may ignore the message or display a generic message instead of the one returned by the function.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply