Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Delay Form Submission without touching existing code?

I need some JS to execute completely before a form submits. The issue is the form submission truncates the JS code executing. I' m trying to figure out if I can delay a form submission without directly adding code to the form line.
<form action="/responsive/letsgetstarted.html?prod=easy_checking" method="post">

Open in new window

The code would have to be added independently of any page mark-up so it would have to be standalone functionality. There is only one form on this page. I'd prefer JS answer to a jQuery solution.

Thanks!

P.S. I'm using Adobe's DTM -event based rule but I don't think that will matter in this case.
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MJ

ASKER

Hi Alexandre,
I'm not following your example completely? Am I supposed to do some type of settimeout in the function before returning true or just have all my code I want to run in myFunction and then return true afterwards?
Avatar of MJ

ASKER

I'm adding this code at page bottom and I tried DOM Ready but I still get the following error:

Uncaught TypeError: Cannot read property 'addEventListener' of undefined

Not sure why?
Hi mate,
My idea is to put that Javascript you need to execute inside this event, or call it from this event handler.

As long as your code is synchronous, the submission will only take place after it's finished.

On the other hand, if you're doing ajax calls, then it gets a bit more complex.

Btw, simply delaying the submission time is a very bad option. Different clients will take different times to complete, eventually breaking the logic or unnecessarily delaying the submission.

Let me know,
Alex
Avatar of MJ

ASKER

Thanks!