views:

59

answers:

3

Hi, 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.

A: 

Yes, you can. See here.

Serapth
+1  A: 

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.

David Dorward
I do submit my form to a frame. Sorry, forgot to mention this.
ecu
Then, as I said, an onload event will fire.
David Dorward
Ok, I get it, but instead of 'onload' I am using $('#frame).load(function(){}) handle the response. This is a file submiting form, so if I put setMyCookie('name',value_2) inside load() function I would have to wait until the file upload is finished and the response gets back. I need to change the value of the cookie right after the form gets submited. Is it possible in this scenario?
ecu
A: 

You would have to use AJAX to accomplish 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.

EndangeredMassa