Sergey Ka
Sergey Ka

Reputation: 35

Vue js submit function

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

Upvotes: 0

Views: 273

Answers (1)

KingsthwaiteJ
KingsthwaiteJ

Reputation: 494

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.

Upvotes: 1

Related Questions