Results 1 to 4 of 4

Thread: Creating a cookie after executing function

  1. #1
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Creating a cookie after executing function

    Hello there!

    So basically what i want to get is a script that will create a cookie after a function is executed. But on forehand it will be checked if that cookie already exists. When this is the case i don't want the function to be executed.

    Code:
    <script type="text/javascript">
    function csearch(DBadblockblocker) {
      var str = document.cookie;
      var zstr = DBadblockblocker + "=";
      var start = str.indexOf(zstr, 0);
      if(start == -1) {
        // If cookie is not detected -> Check if Adblock is enabled
            adblockcheck();
            // After the popup a cookie called DBadblockblocker met value true word aangemaakt
            document.cookie = "DBadblockblocker=true;path=/";
            alert('Cookie is aangemaakt');
            // Give alert when cookie has been made
      } else {
        alert('Cookie bestond al');
            // Give alert when cookie already excists
      }
    }
    </script>
    I've added these 2 alerts because it was not working, but i just can't find the problem.

    Hope you guys can help me!
    Last edited by CasJ; 11-16-2012 at 09:05 PM.

  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>
    <script type="text/javascript">
    function csearch(nme){
     if (!cookie(nme)){
      alert('set the cookie');
      var days=1;
      document.cookie=nme+'=true;expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';
     }
     else {
      alert('the cookie has been set');
     }
    }
    
    function cookie(nme){
      var re=new RegExp(nme+'[^;]+','i');
      if (document.cookie.match(re)){
       return document.cookie.match(re)[0].split("=")[1];
      }
      return null
     }
    
    
    </script>
    
    </head>
    
    <body>
    <input type="button" name="" value="test" onmouseup="csearch('nme');"/>
    </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
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Code:
     var start = str.indexOf(zstr, 0);
    document.cookie may contain other data other than that you set, therefore the above test is flawed.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Also, having a global object named cookie can conflict with, well I'm not sure, it's either document.cookie or perhaps they have a global already or perhaps it's just a reserved word, etc. In any case, in some browsers it won't work.

    See:

    http://www.dynamicdrive.com/forums/e...ct-Cookie-Unit

    for my take on adding/reading/managing cookies.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. cookie get value function not working.
    By antonyf in forum JavaScript
    Replies: 1
    Last Post: 11-05-2011, 03:29 PM
  2. Creating a cookie (with expiry date)
    By Macca in forum JavaScript
    Replies: 3
    Last Post: 09-15-2010, 11:38 AM
  3. Executing function from popup
    By JBottero in forum JavaScript
    Replies: 0
    Last Post: 03-12-2010, 07:50 PM
  4. Need help with cookie background function..
    By ShadowIce in forum JavaScript
    Replies: 12
    Last Post: 05-20-2009, 10:00 PM
  5. Add cookie before executing script
    By monkeyboy in forum JavaScript
    Replies: 0
    Last Post: 01-24-2006, 05:39 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
  •