Results 1 to 4 of 4

Thread: Creating a cookie

  1. #1
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default Creating a cookie

    I would like to add a cookie to this code but without having to use the jQuery cookie plugin. I have searched on Google for hours and couldn't find anything else. Could any one else please help?
    An inline div is a freak of the web and should be beaten until it becomes a span

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    enter a value and click a button
    <input id="test" />
    <input type="button" name="" value="SetCookie Days" onclick="SetCookie('test',document.getElementById('test').value,2)" />
    <input type="button" name="" value="SetCookie Session" onclick="SetCookie('test',document.getElementById('test').value,null)" />
    <input type="button" name="" value="Cancel SetCookie" onclick="SetCookie('test',document.getElementById('test').value,-1)" />
    
    <script type="text/javascript">
    
    function GetCookie(nme){
     var v=Cookie(nme);
     if (v&&document.getElementById(nme)){
      document.getElementById(nme).value=v;
     }
    }
    
    function Cookie(nme){
     var re=new RegExp(nme+'=[^;]+','i');
     return document.cookie.match(re)?document.cookie.match(re)[0].split("=")[1]:null;
    }
    
    function SetCookie(n,v,days){
     document.cookie=n+'='+v+(typeof(days)=='number'?';expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path/;':';');
    }
    
    GetCookie('test');
    
    </script>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Feb 2015
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    u must add the cookie following function,,, value of each content using jquery
    function SetCookie(cookieName,cookieValue,nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays==null || nDays==0) nDays=1;
    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName+"="+escape(cookieValue)
    + ";expires="+expire.toGMTString();
    }

  4. #4
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    Appreciate the help, but I still dont know what to do with the code you provided. Here is the jQuery Code that I have that needs a cookie
    Code:
    $(document).ready(function() {
      $('.day-night').click(function() {
        $('body').toggleClass('night');
      });
    });
    An inline div is a freak of the web and should be beaten until it becomes a span

Similar Threads

  1. Creating a cookie after executing function
    By CasJ in forum JavaScript
    Replies: 3
    Last Post: 11-18-2012, 07:02 PM
  2. [PHP] Help with cookie
    By Lestatt in forum PHP
    Replies: 3
    Last Post: 06-01-2012, 01:05 PM
  3. Creating a cookie (with expiry date)
    By Macca in forum JavaScript
    Replies: 3
    Last Post: 09-15-2010, 11:38 AM
  4. Changing from session cookie to permanent cookie
    By boisemedia in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 03-21-2008, 10:07 PM
  5. Replies: 4
    Last Post: 12-04-2007, 10:52 PM

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
  •