...

View Full Version : cookie help



rubenbuhr
10-22-2006, 01:49 AM
Hi, I got a generic cookie script some time ago and have worked with it as much as I know how. I've use it on a few pages on my computer so much that I use this line over and over again...

<SCRIPT src="globalCookie.js"></SCRIPT>

That makes the code on my page a lot easier and less cluttered. But here is the cookie script it refers too;


var expDate = new Date();

function setCookie(isName,isValue,daysExpire){
if(daysExpire){ expDate.setTime(expDate.getTime()+24*60*60*1000*daysExpire); }
else{expDate.setTime(expDate.getTime()+24*60*60*1000*360);}
document.cookie = isName+"="+isValue+";expires="+expDate.toGMTString();
}

function getCookie(isName){
var cookieStr = document.cookie;
var startSlice = cookieStr.indexOf(isName+"=");

if (startSlice == -1){return false}
var endSlice = cookieStr.indexOf(";",startSlice+1);
if (endSlice == -1){endSlice = cookieStr.length}
var isData = cookieStr.substring(startSlice,endSlice);
var isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}

function dispCookie(isName){
var nValue = getCookie(isName);
return (nValue);
}

function deleteCookie(isName){
if (getCookie(isName)){document.cookie = isName + "="+
"; expires=Thu, 01-Jan-70 00:00:01 GMT";}
}



Ok to demonstrate the problem, here is the HTML;
<html>
<head> <title>kookieDemo </title>
<STYLE type="text/CSS">

</STYLE>
<SCRIPT src="globalCookie.js"></SCRIPT>

<script type="text/javascript">

function fetch(){
k=dispCookie('testCook')
document.getElementById('MyTest').value=k
}
</script>
</head>
<body onload="fetch()">


<button onclick="setCookie('testCook',document.getElementById('MyTest').value,5)">SaveNote</button><br>
<textarea ID="MyTest" ></textarea>

</body>
</html>


It works great. The third argument of "setCookie()" can be left off, it identifies how many days until expiration (default is 360 days.) The problem is, the cookie can't handle a line break in the textarea. It must break on it's own at a line space or it gets corrupted. I know this is going to be a major headache for me just to see how you'll fix it? Have mercy, I just don't know what to do?

Arty Effem
10-22-2006, 03:42 AM
It works great. The third argument of "setCookie()" can be left off, it identifies how many days until expiration (default is 360 days.) The problem is, the cookie can't handle a line break in the textarea. It must break on it's own at a line space or it gets corrupted. I know this is going to be a major headache for me just to see how you'll fix it? Have mercy, I just don't know what to do?
Try changing the parameter to:
document.getElementById('MyTest').value.replace(/\r|\n/g," ") which should convert all newlines to spaces.

jmrker
10-22-2006, 04:09 AM
'Arty Effem' found the answer before I could respond.

I took the problem a bit further by converting the 'returns' to a sentinel
character(s) in the 'globalCookie.js' file and returning them to the original
position later when the information is collected.

I added a few buttons to test the final version, but you can remove them
if they are not desired.

globalCookie.js


// globalCookie.js
var expDate = new Date();

function setCookie(isName,isValue,daysExpire) {
if (daysExpire) { expDate.setTime(expDate.getTime()+24*60*60*1000*daysExpire); }
else {expDate.setTime(expDate.getTime()+24*60*60*1000*360); }
isValue = isValue.replace(/\n/g,"~~");
document.cookie = isName+"="+isValue+";expires="+expDate.toGMTString();
}

function getCookie(isName) {
var cookieStr = document.cookie;
var startSlice = cookieStr.indexOf(isName+"=");

if (startSlice == -1) {return false}
var endSlice = cookieStr.indexOf(";",startSlice+1);
if (endSlice == -1) { endSlice = cookieStr.length; }
var isData = cookieStr.substring(startSlice,endSlice);
var isValue = isData.substring(isData.indexOf("=")+1,isData.length);
isValue = isValue.replace(/~~/g,"\n");

return isValue;
}

function dispCookie(isName) {
var nValue = getCookie(isName);
return (nValue);
}

function deleteCookie(isName) {
if (getCookie(isName)) {
document.cookie = isName + "="+"; expires=Thu, 01-Jan-70 00:00:01 GMT"; }
}


globalCookie.html


<html>
<head> <title>kookieDemo </title>
<STYLE type="text/CSS"></STYLE>
<SCRIPT src="globalCookie.js"></SCRIPT>

<!-- Following is not used in this version
<script type="text/javascript">
function fetch(){
k=dispCookie('testCook')
document.getElementById('MyTest').value=k
}
</script>
-->

</head>
<!-- body onload="fetch()" --><!-- Use to automatically fetch saved cookie -->

<body>
<button onclick="setCookie('testCook',document.getElementById('MyTest').value,5)">Save Note</button>
<button onclick="getCookie('testCook',document.getElementById('MyTest').value,5)">Get Note</button>
<button onclick="document.getElementById('MyTest').value=''">Clear Note</button>
<button onclick="document.getElementById('MyTest').value=dispCookie('testCook')">Display Note</button>
<button onclick="deleteCookie('testCook',document.getElementById('MyTest').value,5)">Delete Note</button>
<br>
<textarea ID="MyTest" ></textarea>
</body>
</html>

rubenbuhr
10-22-2006, 05:32 AM
Well, ok and thanks for the help. Really.
There is a part of the code I still don't savvy but it started to make sense when I remembered using replace() once before and found the snippet. Firefox and IE handle things differently and I was lucky to find one short line of cheat. Since I use the globalCookie.js on enough pages that I wish to leave it as-is if possible so, I only edited the HTML so that I can hit carriage returns (enter)

<html>
<head> <title>kookieDemo </title>
<STYLE type="text/CSS">

</STYLE>
<SCRIPT src="globalCookie.js"></SCRIPT>

<script type="text/javascript">

function fetch(){
k=dispCookie('testCook')

document.getElementById('MyTest').value=k
ReplaceText('~~','\n');
ReplaceText('~','\n')
}

function ReplaceText(HH,ZZ){
while(document.getElementById('MyTest').value.indexOf(HH)!=-1){
document.getElementById('MyTest').value = document.getElementById('MyTest').value.replace(HH,ZZ);
}
}

</script>
</head>
<body onload="fetch()">

<button onclick="setCookie('testCook',document.getElementById('MyTest').value.replace(/\r|\n/g,'~'),5)">SaveNote</button><br>
<textarea ID="MyTest" rows=10></textarea>

</body>
</html>

The part I don't savvy is where the cookie is set, replace(/\r|\n/g,'~')
IE will give me back 2~~ and firefox only 1~
so, what is going on in that red part of the code I highlighted?

VortexCortex
10-22-2006, 07:06 AM
IE uses Windows line breaks... Both 0x0D and 0x0A.

The new line and carriage return... FF uses only new lines.

do this:

.replace(/(\n\r)|\r|\n/g,'~');

Mr J
10-22-2006, 08:17 AM
Returns and line breaks should be automatically saved by the cookie
See if you have the same problem with this example?


<HTML>
<HEAD>
<TITLE>Document Title</TITLE>

<script type="text/javascript">

cookieName="thenotepad"
days=7

function saveDataToCookie(){ // compile data for cookie

var expiryDate = new Date()
expiryDate.setTime(expiryDate.getTime() + (days*24*60*60*1000))

cookieString=document.calc.notepad.value

setCookie(cookieName,cookieString,expiryDate)
}

function getCookieData(){ // decompile data from cookie
cookieContent=getCookie(cookieName)
if(!cookieContent){return}

retrievedData=cookieContent

document.calc.notepad.value = retrievedData

}

// An adaptation of Dorcht's cookie functions.

function setCookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure")
}

function getCookie(name) {
var arg = name + "="
var alen = arg.length
var clen = document.cookie.length
var i = 0
while (i < clen) {
var j = i + alen
if (document.cookie.substring(i, j) == arg){
return getCookieVal(j)
}
i = document.cookie.indexOf(" ", i) + 1
if (i == 0) break
}
return null
}

function getCookieVal(offset){
var endstr = document.cookie.indexOf (";", offset)
if (endstr == -1)
endstr = document.cookie.length
return unescape(document.cookie.substring(offset, endstr))
}

function deleteCookie(name,path,domain){
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT"
}

</script>
</HEAD>
<BODY>

<form name="calc">
<textarea name="notepad" id="notepad" rows="5" cols="40"></textarea>

<input type="button" value="Save" onclick="saveDataToCookie()">
<input type="button" value="Open" onclick="getCookieData()">
<input type="button" value="Delete Cookie" onclick="deleteCookie('thenotepad')">
</form>

</BODY>
</HTML>

Mr J
10-22-2006, 08:37 AM
I think I've found where the problem lies in your cookie script, you are not escaping and unescaping the values


This line in the set cookie function

document.cookie = isName+"="+isValue+";expires="+expDate.toGMTString();

should be

document.cookie = isName+"="+escape(value)+";expires="+expDate.toGMTString();

and this line in the getCookie function

return isValue;

should be

return unescape(isValue)

rubenbuhr
10-22-2006, 04:12 PM
Thank you Mr J, :thumbsup:

rubenbuhr
10-28-2006, 11:00 PM
Here's what I ended with for show & tell.The big change was in this "globalCookie.js" (thats what you name and save it as)



var expDate = new Date();

function setCookie(isName,isValue,daysExpire){
if(daysExpire){ expDate.setTime(expDate.getTime()+24*60*60*1000*daysExpire); }
else{expDate.setTime(expDate.getTime()+24*60*60*1000*360);}
document.cookie = isName+"="+escape(isValue)+";expires="+expDate.toGMTString();
}

function getCookie(isName){
var cookieStr = document.cookie;
var startSlice = cookieStr.indexOf(isName+"=");

if (startSlice == -1){return false}
var endSlice = cookieStr.indexOf(";",startSlice+1);
if (endSlice == -1){endSlice = cookieStr.length}
var isData = cookieStr.substring(startSlice,endSlice);
var isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return unescape(isValue);
}

function dispCookie(isName){
var nValue = getCookie(isName);
return (nValue);


}

function deleteCookie(isName){

if (getCookie(isName)){document.cookie = isName + "="+
"; expires=Thu, 01-Jan-70 00:00:01 GMT";}
}



There is a function in the .js to delete the cookie. I don't show a button to do it here in the HTML demo--

<html>
<head> <title>kookieDemo </title>
<STYLE type="text/CSS">

</STYLE>
<SCRIPT src="globalCookie.js"></SCRIPT>

<script type="text/javascript">

function fetch(){
k=dispCookie('testCook')
document.getElementById('MyTest').value=k
}
</script>
</head>
<body onload="fetch()">


<button onclick="setCookie('testCook',document.getElementById('MyTest').value,5)">SaveNote</button>(5 day cookie)<br>
<textarea ID="MyTest" rows=10></textarea>

</body>
</html>


You can paste the working parts into your HTML as long as the path to the .js is correct. Pasting it from the IE address window will be wrong while the path given in FireFox would be correct. Here is the path Fifrefox gives me--- file:///C:/Documents%20and%20Settings/admin/My%20Documents/globalCookie.js

You can start by saving both file demonstrations to your desktop

Tested to work in IE and Firefox browsers

hiddenbox
10-29-2006, 12:03 AM
I was looking for that Note code!


This code works Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum