0

There is code to handle the button to close the page and initialize submit

window.onbeforeunload = function(e) {
  var dialogText = 'Dialog text here';
  e.returnValue = dialogText;
  return dialogText;
};

How to use JavaScript to process this function in Vue JS to send the form through HTML tags? I did similar things on Jinja, but I can specify data directly in the URL

1 Answer 1

1

You need to bind that function to the beforeunload window event when the component is initialized.

Example:

mounted() {
    window.addEventListener("beforeunload", this.unload);
},

methods: {
    unload(e) {
        var dialogText = 'Dialog text here';
        e.returnValue = dialogText;
        return dialogText;
    }
}

Alternatively, if you're using the form element with a submit function, you could just call unload() directly? Hard to know whether that would simplify the solution without seeing more of your code.

Sign up to request clarification or add additional context in comments.

3 Comments

I need send data in unload func, but it not work. Code and log: pastebin.com/HkQS5KYe
Have you tried awaiting the this.test() function and the axios call inside test()? It looks like the output of the request will never be logged because you're not waiting for the request to finish before moving on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.