Store/Retrieve Cookie Values

[Total: 0    Average: 0/5]

This example stores 3 variables into a cookie, and then retrieves them.

Head Code:

<SCRIPT type="text/javascript" src="http://www.js-examples.com/bin/cookies.js"></SCRIPT>
<script language="JavaScript">
<!-- //
function create(data) {
var _p  = "toolbar=yes";
    _p += ",location=no";
    _p += ",width=500";
    _p += ",height=400";
    _p += ",directories=yes";
    _p += ",status=yes";
    _p += ",scrollbars=yes";
    _p += ",resize=yes";
    _p += ",menubar=yes"
var _name = "newWin";
var _obj = window.open("",_name,_p);
  var _d  = "";
      _d += "<BR>";
	  _d += "<BODY bgcolor=white>";
	  _d += "<FONT face=Arial,Helvetica,Geneva,SunSans-Regular>";
	  _d += "<DIV align=center>";
	  _d += "<B>";
      _d += "IN1="+data.in1.value;
	  _d += "<BR>";
      _d += "IN2="+data.in2.value;
	  _d += "<BR>";
      _d += "IN3="+data.in3.value;
	  _d += "</B>";                    
	  _d += "<BR>";
	  _obj.document.write(_d);                
} 
// --> 
</script>

Body Code:

<SCRIPT type="text/javascript">
var IN1="";
var IN2="";
var IN3="";
 var myCookie = new Cookie(document,"myName",0); // 0 (last parmeter) is the number of days until it expires.
 if (myCookie.read() && myCookie.good) { 
   IN1 = myCookie.IN1 || "";
   IN2 = myCookie.IN2 || "";
   IN3 = myCookie.IN3 || "";
 } 
// myCookie.write() 
var _form =  '';
    _form += '<form>';
	_form += 'IN1: <input type=text name=in1 value="'+IN1+'" size=10>';
	_form += '<BR>';
	_form += 'IN2: <input type=text name=in2 value="'+IN2+'" size=10>';
	_form += '<BR>';
	_form += 'IN3: <input type=text name=in3 value="'+IN3+'" size=10>';
	_form += '<BR>';
	_form += '<input type=button value=Done onclick="myCookie.write();create(this.form)">';
	_form += '</form>';
document.write(_form);
</SCRIPT>

Times Viewed: 2

Leave a Reply

Your email address will not be published. Required fields are marked *