HTML Form Submits Twice If The Onsubmit Function Runs Longer Than 10 Seconds
Solution 1:
Improvement suggestions
- Replace inline event handlers (
onsubmit=
andonclick=
attributes) by event listeners - Use
withSuccessHandler
andwithFailureHandler
when usinggoogle.script.run
- Add
google.script.host.close()
inside the callback ofwitchSuccessHandler
- Add the following at the beginning of your
<script></script>
// Prevent forms from submitting.
function preventFormSubmit() {
var forms = document.querySelectorAll('form');
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener('submit', function(event) {
event.preventDefault();
});
}
}
window.addEventListener('load', preventFormSubmit);
From https://developers.google.com/apps-script/guides/html/communication#forms
Note that upon loading all forms in the page have the default submit action disabled by preventFormSubmit. This prevents the page from redirecting to an inaccurate URL in the event of an exception.
You can find HTML attribute equivalents for many of the event handler properties; however, you shouldn't use these — they are considered bad practice. It might seem easy to use an event handler attribute if you are just doing something really quick, but they very quickly become unmanageable and inefficient.
Please bear in mind that google.script.run
calls are asynchronous (the code after them will be executed before the server side function ends)
Related
Post a Comment for "HTML Form Submits Twice If The Onsubmit Function Runs Longer Than 10 Seconds"