Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: cookie help

  1. #1
    Regular Coder
    Join Date
    Oct 2006
    Posts
    107
    Thanks
    3
    Thanked 5 Times in 5 Posts

    cookie help

    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?

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by rubenbuhr View Post
    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:
    Code:
    document.getElementById('MyTest').value.replace(/\r|\n/g," ")
    which should convert all newlines to spaces.

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts

    Smile Another version for your consideration.

    '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
    PHP Code:
    // 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
    PHP Code:
    <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> 

  • #4
    Regular Coder
    Join Date
    Oct 2006
    Posts
    107
    Thanks
    3
    Thanked 5 Times in 5 Posts
    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?

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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,'~');

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Returns and line breaks should be automatically saved by the cookie
    See if you have the same problem with this example?

    PHP Code:
    <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(namevalueexpirespathdomainsecure){
    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 0
    while (clen) {
    var 
    alen
    if (document.cookie.substring(ij) == arg){
    return 
    getCookieVal(j)
    }
    document.cookie.indexOf(" "i) + 1
    if (== 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(offsetendstr))
    }

    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> 
    Last edited by Mr J; 10-22-2006 at 09:41 AM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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)
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #8
    Regular Coder
    Join Date
    Oct 2006
    Posts
    107
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Thank you Mr J,

  • #9
    Regular Coder
    Join Date
    Oct 2006
    Posts
    107
    Thanks
    3
    Thanked 5 Times in 5 Posts
    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

  • #10
    New Coder
    Join Date
    Sep 2006
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was looking for that Note code!


    This code works Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •