Reputation: 1481
When the button is clicked, I want to ask a series of messages and give the user a secondary confirmational option. However it doesn't work.
<html>
<body>
<p>Close the page to trigger the onunload event.</p>
<script type="text/javascript">
var changes = false;
window.onbeforeunload = function() {
if (changes)
{
var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
if (confirm(message)) return true;
else return false;
}
}
</script>
<button onClick='function;'> </input>
</body>
</html>
Upvotes: 0
Views: 80
Reputation: 110
Just add the "e" inside the function() and the variable changes.
Before: window.onbeforeunload = function() {
After: window.onbeforeunload = function(e) {
<html>
<body>
<p>Close the page to trigger the onunload event.</p>
<script type="text/javascript">
window.onbeforeunload = function(e) {
var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
if (confirm(message)) return true;
else return false;
}
</script>
<button onClick='window.close();'> </input>
</body>
</html>
Upvotes: 1