SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Saving data with cookies

    Hi,

    I'm working on a webpage that has drag-and-drop tabular data. I built the page with html/css (I don't know how to program or use a database) and uses jQuery for the drag-and-drop feature. Is there a way that I could use cookies to save the data each time it is changed via drag-and-drop?

  2. #2
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function setCookie(c_name, value, expiredays) {
      var exdate=new Date();
      exdate.setDate(exdate.getDate()+expiredays);
      document.cookie=c_name+ "=" +escape(value)+
      ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
    }
    
    function getCookie(c_name) {
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1) {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
          return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
    }
    Those two functions will allow you to save and load a cookie. However, you will need to keep in mind that cookies are not limitless storage devices -- you can hold approximately 4K of data per cookie. Also, cookies are sent up to the server in the header of each request, and some web servers may return an error if your header size exceeds a preset amount, so you don't want to just keep adding additional 4K cookies.


Bookmarks

Posting Permissions

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