Is it possible to store a javascript object (i.e. all the elements from a form and their values) in a cookie?
If not, can anyone please suggest a way of performing this task, please??
Is it possible to store a javascript object (i.e. all the elements from a form and their values) in a cookie?
If not, can anyone please suggest a way of performing this task, please??
Hi,
I think you can't submit any object directly to cookie. You must
first convert in a string, and back to object when you read it...
p.s:what is today? the "cookie's day" ?
there is another one here...
http://www.phpbuilder.com/board/showthread.php?s=&threadid=10249068
Wow, a little bug on my
script make me crazy, but finally it works. Look:
<HTML>
<SCRIPT>
function customObj()
{
customObj.m_value="";
customObj.m_text="";
customObj.prototype.obj2str=f_obj2str;
customObj.prototype.str2obj=f_str2obj;
function f_obj2str()
{
mytext=this.m_value + this.m_text;
return mytext;
}
function f_str2obj(p_txt)
{
this.m_value=p_txt.substring(0,2)
this.m_text=p_txt.substring(2,p_txt.length)
}
}
var yourname = new customObj();
function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}
function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) { offset = document.cookie.indexOf(search)
if (offset != -1) { offset += search.length
end = document.cookie.indexOf(";", offset)
if (end == -1) end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
} return(" ")
}
function register() {
var today = new Date()
var expires = new Date()
mytext=yourname.obj2str();
expires.setTime(today.getTime() + 60*60*24*365)
setCookie("TheCoolJavaScriptPage", mytext, expires)
}
</SCRIPT>
<BODY>
<H1>Register Your Name with the Cookie-Meister</H1>
<SCRIPT>
mytext=getCookie("TheCoolJavaScriptPage");
yourname.str2obj(mytext);
if (mytext != " ") {
document.write("<P>Welcome Back, ", yourname.m_text, yourname.m_value);
} else {
document.write("<P>You haven't been here in the last year...");
}
</SCRIPT>
<P> Enter your name. When you return to this page within a year, you will be greeted with a personalized greeting. <BR>
<FORM name="mform" onSubmit="return false">
Enter your name: <INPUT TYPE="text" NAME="username" SIZE= 10 ALT="Welcome"><BR>
<INPUT TYPE="button" value="Register" onClick="yourname.m_value=13; yourname.m_text=this.form.username.value; register(); history.go(0)">
</FORM>
</P>
</BODY>
</HTML>
Put an object in cookie and read cookie in object... but to do this
transform all in a reconvertible string.