How to handle beforeunload events while writing

Asked 2 years ago, Updated 2 years ago, 52 views

You can handle it like this when you leave the window.

$(window).bind("beforeunload", function (e){
    You may lose the content of "return". Would you still like to go out?";
});

How do you handle an event if you leave a page or return to a list while writing?

jquery

2022-09-22 18:02

2 Answers

If there is any input form in the window.document, I don't know if this material will help. (Search word: js confirm before leaving the form)

// Bind window.beforeunload event first
$(window).on("beforeunload", function() {
    You may lose your "return" content. Would you still like to go out?";
});
$(document).ready(function() {
    // If there's a submission event in a form,
    $("#myForm").on("submit", function (e) {
        // I'll untie the window.beforeunload binding so that it's okay to go out anytime
        $(window).off("beforeunload");
        // Submit a form
        return true;
    });
});


2022-09-22 18:02

onbeforeunload or onclose is an event where browser support may not be available, so use onunload.

https://developer.mozilla.org/ko/docs/Web/API/WindowEventHandlers/onunload

When a user tries to move a page, it seems that he/she is trying to block the movement and print out a message.

I understand that it is impossible to block the page movement due to scripts in most browsers. I think it's probably because it can be abused on spam sites.


2022-09-22 18:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.