Web Analytics Made Easy -
StatCounter Notepad cookies; can't save multiple lines - CodingForum

Announcement

Collapse
No announcement yet.

Notepad cookies; can't save multiple lines

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Notepad cookies; can't save multiple lines

    I created a notepad of sorts using Javascript (using code for cookies from http://www.quirksmode.org/js/cookies.html). It works fine.. except that it cannot save multiple lines.

    In Firefox and Opera, when the value of the textarea is changed to the cookie value, only the first line is displayed (before any line breaks). In IE, each line break is simply replaced by two underscores ("__"), so all lines can be seen but they are on a single line.

    Is there any solution to this problem? My Javascript and HTML code is below.

    PHP Code:
    function createCookie(name,value,days) {
        if (
    days) {
            var 
    date = new Date();
            
    date.setTime(date.getTime()+(days*24*60*60*1000));
            var 
    expires "; expires="+date.toGMTString();
        }
        else var 
    expires "";
        
    document.cookie name+"="+value+expires+"; path=/";
    }

    function 
    readCookie(name) {
        var 
    nameEQ name "=";
        var 
    ca document.cookie.split(';');
        for(var 
    i=0;ca.length;i++) {
            var 
    ca[i];
            while (
    c.charAt(0)==' 'c.substring(1,c.length);
            if (
    c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return 
    null;
    }

    function 
    eraseCookie(name) {
        
    createCookie(name,"",-1);
    }

    function 
    writeNotepad() {
        var 
    notes unescape(readCookie('notepad'));
        if (
    notes) {
            
    document.calc.notepad.value notes;
        }

    PHP Code:
    <td colspan="2">
    <
    textarea name="notepad" id="notepad" rows="5" cols="40"></textarea>
    </
    td>
    </
    tr>
    <
    tr>
    <
    td><input type="button" value="Save" onclick="createCookie('notepad',document.calc.notepad.value,7)" /></td>
    <
    td><input type="button" value="Open" onclick="writeNotepad()" /></td
    Any help would be greatly appreciated.

  • #2
    Solved the problem in IE and Opera by escaping the value when it is written, but now the cookie doesn't even save in Firefox.

    Line changed:
    Code:
    document.cookie = name+"="+escape(value)+expires+"; path=/";

    Comment


    • #3
      Give this a try

      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()" />
      </
      form>
      </
      BODY>
      </
      HTML
      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.

      Comment


      • #4
        Works perfectly in all browsers, but would you mind telling me how it's different from my code and how to adapt my current code to work? I don't like to simply copy and paste code without learning anything.

        Comment


        • #5
          Have a read here, it may be of help

          http://www.huntingground.freeserve.c...the_cookie.htm
          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.

          Comment

          Working...
          X