admin管理员组

文章数量:1320589

I have this code, and a file upload form which is submited to a frame:

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

Problem: Webkit browsers seem to update 'MyCookie' with 'value_2' before the form gets submited, or in the exact moment it is being submited, so wrong cookie value is sent with it. I want to change cookie value to 'value_2' immediately after the form is submited so 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.

I have this code, and a file upload form which is submited to a frame:

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

Problem: Webkit browsers seem to update 'MyCookie' with 'value_2' before the form gets submited, or in the exact moment it is being submited, so wrong cookie value is sent with it. I want to change cookie value to 'value_2' immediately after the form is submited so 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.

Share Improve this question edited Jun 17, 2010 at 19:48 ababa asked Jun 17, 2010 at 19:18 ababaababa 1,8734 gold badges16 silver badges14 bronze badges 3
  • I think the question does not match your description of your problem, if I understand this correctly. This sounds like a WebKit bug to me, as JavaScript execution should stop on submitting a form, like David said. – Marcel Korpel Commented Jun 17, 2010 at 19:26
  • I submit my form to a frame, so javascript execution will not stop. – ababa Commented Jun 17, 2010 at 19:29
  • That's not a reliable way using a delay technique like that (calling setTimeout()) – Kevin Le - Khnle Commented Jun 17, 2010 at 19:37
Add a ment  | 

3 Answers 3

Reset to default 3

No. Submitting a form loads an entirely new page, which usually ends the current execution context for the script.

The exception is if you submit the form to a frame, in which case an onload event will fire.

Yes, you can. See here.

You would have to use AJAX to acplish that. Otherwise, the form is submitted and the rest of the JavaScript that your function was going to execute is ignore because the page is reloaded.

本文标签: javascriptDoes submit() function has a callbackStack Overflow