in reply to Execute javascript before setting cookie headers
You could try making the javascript redirect to another page after it's done its work, and that other page can set the cookies.