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

Sergey Ka
  • 25
  • 9

1 Answers1

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.

KingsthwaiteJ
  • 464
  • 4
  • 10
  • I need send data in unload func, but it not work. Code and log: https://pastebin.com/HkQS5KYe – Sergey Ka Oct 19 '21 at 17:28
  • 1
    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. – KingsthwaiteJ Oct 20 '21 at 19:51
  • this helped me: https://stackoverflow.com/a/69647485/16067483 – Sergey Ka Oct 21 '21 at 06:06