Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what is wrong with this?

Status
Not open for further replies.

iLy54

Programmer
Sep 5, 2002
33
0
0
ZA
this is part of my code:


// cookie functions
function getCookieExpireDate(noDays){
var today = new Date()
var expr = new Date(today.getTime()+31*24*60*60*1000)
return expr.toGMTString()
}
function makeCookie(name, data, noDays){
var cookieStr = name + "="+ data
if (makeCookie.arguments.length > 2){
cookieStr += "; expires=" + getCookieExpireDate(noDays)
}
document.cookie = cookieStr
}


this should set the cookie to exist for a month. but instead it only lasts as long as the browser is open. the cookie is accesed on a separate page. If i close the browser and redirect straight to the page where the cookie is read, there is no data values.
what am i doing wrong?
-Mon3y is the r00t of all evil and every man needs roots-
 
The code below is a copy and paste example of cookies in asp and javaclientscript. The cookie is set with a name, array of item names and an array of item values.
An expire date is not given as it is fixed on current date + 50 months.





<%@enablesessionstate=&quot;false&quot; language=&quot;vbscript&quot;%>
<% ' @enablesessionstate is to disable session %>
<% Response.CacheControl = &quot;no-cache&quot; %>
<% Response.AddHeader &quot;Pragma&quot;, &quot;no-cache&quot; %>
<% Response.Expires = -1 %>
<html>
<head></head>
<body>
<%
dim lng_Count
if Request.Cookies(&quot;CartItem1&quot;)(&quot;ItemID&quot;) = &quot;&quot; then
'simulate a 'cart of items
randomize
for lng_Count = 1 to 10
Response.Cookies(&quot;Cart;Item&quot; & lng_Count)(&quot;ItemID&quot;) = &quot;GLS;&quot; & (330 + lng_Count)
Response.Cookies(&quot;Cart;Item&quot; & lng_Count)(&quot;ItemName&quot;) = &quot;VisionCo Glasses &quot; & lng_Count
Response.Cookies(&quot;Cart;Item&quot; & lng_Count)(&quot;ItemCost&quot;) = round(rnd*10+1,2)
Response.Cookies(&quot;Cart;Item&quot; & lng_Count)(&quot;ItemQuantity&quot;) = int(rnd*3)
'etc
next
else
lng_Count = 1
while Request.Cookies(&quot;CartItem&quot; & lng_Count)(&quot;ItemID&quot;) <> &quot;&quot;
Response.Write Request.Cookies(&quot;CartItem&quot; & lng_Count)(&quot;ItemID&quot;)%><br>
<%=Request.Cookies(&quot;Cart;Item&quot; & lng_Count)(&quot;ItemName&quot;)%><br>
<%=Request.Cookies(&quot;Cart;Item&quot; & lng_Count)(&quot;ItemCost&quot;)%><br>
<%=Request.Cookies(&quot;Cart;Item&quot; & lng_Count)(&quot;ItemQuantity&quot;)%><br>
<% lng_Count = lng_Count + 1
wend
end if
%>
<textarea style=&quot;width:600;height:400&quot; id=t1 name=t1></textarea>
<script>
function cookies(strName) {
}

cookies.prototype.del =function(strName){
// to delete a cookie make a string like so:
// cookiename=; expires= dateInThePast.toGMTString()
// this wil return true if the cookiename does not exsist
var blnReturn = false;
var expires = new Date();
expires.setMonth(expires.getMonth()-1);
document.cookie = escape(strName) + &quot;=gone; expires=&quot; + expires.toGMTString();
return !this.get(strName,&quot;&quot;)[0];
}
cookies.prototype.add =function(strName,arrItemNames,arrItemValues){
// to create a cookie:
// make a string like this:
// name=value value should be a set of names and values
// name=nameOfValue1=value1&nameOfValue2=value2
// this wil return true if the cookiename exsists
if(arrItemNames.length!=arrItemValues.length||arrItemNames.length==0) { return false;}
var expires = new Date();
var strCookie = escape(strName) + &quot;=&quot;;
expires.setMonth(expires.getMonth()+50);
for(i=0;i<arrItemNames.length;i++) {
strCookie = strCookie + escape(arrItemNames[ i ]) + &quot;=&quot; + escape(arrItemValues[ i ]) + &quot;&&quot;;
}
strCookie = strCookie.substring(0,strCookie.length-1);
this.del(strName);
document.cookie = strCookie + &quot;; expires=&quot; + expires.toGMTString();
return this.get(strName,&quot;&quot;)[0];
}
cookies.prototype.change =function(strName, strItemName, strValue){
// this wil true true if the strName and strItemName exist.
//
var arrRet = this.get(strName,strItemName);
var arrTemp = new Array();
var arrItemNames = new Array();
var arrItemValues = new Array();
var strTemp = &quot;&quot;;
if(arrRet.length==2) {
return arrRet;
}
arrTemp = document.cookie.split(&quot;; &quot;);
for(i=0;i<arrTemp.length;i++){
if(arrTemp[ i ].substring(0,strName.length)==strName) {
arrTemp=arrTemp[ i ].substring(strName.length+1,arrTemp[ i ].length).split(&quot;&&quot;);
for(j=0;j<arrTemp.length ;j++){
arrItemNames[j]=unescape(arrTemp[j].split(&quot;=&quot;)[0]);
arrItemValues[j]=unescape(arrTemp[j].split(&quot;=&quot;)[1]);
}
break;
}
}
for(i=0;i<arrItemNames.length;i++) {
if(arrItemNames[ i ]==strItemName) {
arrItemValues[ i ] = strValue;
break;
}
}
this.add(strName,arrItemNames,arrItemValues)
return this.get(strName,strItemName);
}
cookies.prototype.get =function(strName, strItemName){
// to get a cookie you need the name of the cookie
// and the name of the item of the cookie
// Request.Cookies cookie(cookiename)(itemname) in asp
var strCookie = document.cookie
var arrCookie = document.cookie.split(&quot;; &quot;);
var tmpArr = new Array();
var arrCookieItem = new Array();
var tmpString = &quot;&quot;;
var arrRet = new Array(false,false);
for(i=0;i<arrCookie.length;i++) {
tmpArr = arrCookie[ i ].split(&quot;=&quot;);
if(unescape(tmpArr[0])==strName){
tmpString = tmpArr[0];
tmpArr = arrCookie[ i ].substring(tmpString.length+1,arrCookie[ i ].length).split(&quot;&&quot;);
tmpString = tmpArr.join(&quot;=&quot;);
tmpArr = tmpString.split(&quot;=&quot;);
arrCookieItem = tmpArr;
arrRet[0] = true;
break;
}
//this.del(unescape(tmpArr[ i ]));
}
if(strItemName==&quot;&quot;){arrRet[1]=false;return arrRet;}
if(!arrRet[0]){ //cookie name was not found, check if the name of the cookie item can be found
for(i=0;i<arrCookie.length;i++) {
tmpArr = arrCookie[ i ].split(&quot;=&quot;);
for(j=0;j<tmpArr.length;j++){
if(unescape(tmpArr[j]).substring(unescape(tmpArr[j]).length-strItemName.length,unescape(tmpArr[j]).length)==strItemName) {
arrRet[1] = true;
break;
}
}
}
}
for(i=0;i<arrCookieItem.length;i++) {
if(unescape(arrCookieItem[ i ])==strItemName) {
arrRet[0] = unescape(arrCookieItem[i+1]);
arrRet[1] = true;
arrRet = arrRet.slice(0,1);
break;
}
}
// there is no coockie found so we return false
return arrRet;
}
// we wil makke a cookies object that we can use to make/edit/get and delete cookies
var cookies = new cookies();

// the first function of the cookies object that we wil do is the add
// function this function needs a cookie name
// and 2 arrays an array containing the names of the cookie sub items
// and an array that contains the values of the cokie sub items
// this function wil return true if the cookie name exsists after creating it
var arrItemNames = new Array(&quot;tst item 1&quot;,&quot;tst item 2&quot;);
var arrItemValues = new Array(&quot;tst value item 1&quot;,&quot;tst value item 2&quot;);
alert('cookie add returns: ' + cookies.add('test cookie',arrItemNames,arrItemValues));

// next we wil use the get function of the cookies object, we want to have
// the cookiename/subitem of that cookie, this function returns an array 2 booleans
// if something goes wrong. The first indicates if the cookiename exsists
// and the second indicates if a subitem exsists with the name you provided.
// if the function can find a value it ruturns an array with only 1 item
// so the array.length will be 1 and the value is in array[0]
//alert('Cannot get this cookie because the subitem does not exists\nso this function returns: ' + cookies.get(&quot;test cookie&quot;,&quot;subitem does not exsists&quot;));
//alert('this is better, now the function returns the value : ' + cookies.get(&quot;test cookie&quot;,&quot;tst item 1&quot;)[0]);

// the change function of the cookies object wil change a sertain subitem of a cookie
// this function takes the name of the cookie, name of the subitem
// and the new value of the subitem (value it wil be changed to
// the function returns the same as the function get
//alert('Cannot change cookie because cookiename does not exists\nso this function returns: ' + cookies.change(&quot;this is an incorrect cookie name&quot;,&quot;tst item 2&quot;,&quot;value is errelevant because it is not set&quot;));
//alert('this is better, now the function returns the value : ' + cookies.change(&quot;test cookie&quot;,&quot;tst item 2&quot;,&quot;changed this value&quot;)[0]);

// the last function of the cookies object is del this deletes the cookie
// It takes one variable and that is the cookie name
// it returns true if the cookie name still exsists after executing
// and false if the cookiename does not excists
//alert('This cookie probebly did not exist even before the del was started del wil return true because the cookie does not exist ' + cookies.del(&quot;this is an incorrect cookie name&quot;));
//alert('This cookie does exsist but not anymore after the del function is started so del returns allso true: ' + cookies.del(&quot;test cookie&quot;));


document.getElementById('t1').value = document.cookie
</script>
<input value=&quot;cookie exist&quot; type=button onclick=&quot;alert(cookies.get('tst','item2').join('--'));&quot; id=button1 name=button1>
<input value=&quot;cookie change&quot; type=button onclick=&quot;alert(cookies.change('tst','item2','this is changed item 2'));;&quot; id=button2 name=button2>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top