Exemplo, simulando envio pelo método POST:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function createCookie(name, value, days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
} else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookieData(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
function setObj(eln){
var rr = false;
if( document.all ){
if( document.all[eln] ){
rr = document.all[eln];
}
}else{
if( document[eln] ){
rr = document[eln];
}else{
if( document.getElementById(eln) ){
rr = document.getElementById(eln );
}
}
}
return rr;
}
function GetURLParameters(){
var sstr = getCookieData("frm1");
if (!sstr)
return null;
var arr = sstr.split('&');
if (arr.length < 1 || arr[0] == "")
return null;
var rs = new Array();
for (var i = 0; i < arr.length; i++){
var KeyValuePair = arr[i].split('=');
rs[KeyValuePair[0]] = decodeURI(KeyValuePair[1]);
}
return rs;
}
//p = getCookieData("frm1");
p = GetURLParameters();
console.log(p);
</script>
</head>
<body>
<form id="frm1" name="frm1" action="cookies.html" method="POST">
<input type="text" size="10" value="" name="foo" id="foo" /><br />
<input type="text" size="10" value="" name="bar" id="bar" />
<input type="submit" value="enviar" />
</form>
<script type="text/javascript">
p = GetURLParameters();
if (p)
for (k in p)
if (document.getElementById(k) !== null)
document.getElementById(k).value = p[k];
createCookie("frm1", "", -1);
var obj = setObj("frm1");
obj.addEventListener("submit", function() {
var e = obj.elements;
var d = new Array();
for(i=0; i < e.length; i++){
t = e[i].type;
if(t != "submit")
d[i] = e[i].name+"="+encodeURI(e[i].value);
}
var data = d.join("&");
alert(data);
createCookie("frm1", data, 1);
});
</script>
</body>
</html>
obs: Os exemplos não utilizam códigos crossbrowser. O recomendado é utilizar um framework como JQuery.