Does submit () function have a callback?

I have this code and a file upload form that is sent to the frame:

setMyCookie('name','value_1'); $('.myform').submit(); setMyCookie('name','value_2'); 

Problem: Webkit browsers seem to update โ€œMyCookieโ€ with โ€œvalue_2โ€ before the form is submitted, or the moment it is submitted, so the wrong cookie value is sent with it. I want to change the cookie value to "value_2" immediately after submitting the form so that the cookie is ready for another request.

The following code works fine, but I don't think using timeout () is the best solution. Maybe there is another way to solve this problem?

 setMyCookie('name','value_1'); $('.myform').submit(); setTimeout(function(){setMyCookie('name',value_2);},100); 

Thanks.

+4
source share
3 answers

Not. Submitting the form loads a completely new page, which usually ends the current execution context for the script.

The exception is sending the form to the frame, in which case the onload event fires.

+3
source

Yes, you can. See here.

0
source

You will need to use AJAX for this. Otherwise, the form is submitted, and the rest of the JavaScript that your function executes is ignored as the page reloads.

0
source

Source: https://habr.com/ru/post/1313105/


All Articles