Results 1 to 4 of 4

Thread: jQuery cookie

  1. #1
    Join Date
    Jan 2006
    Location
    Ft. Smith, AR
    Posts
    795
    Thanks
    57
    Thanked 129 Times in 116 Posts

    Default jQuery cookie

    Ok, so i've been racking my brain trying to figure out how to get this to work.

    What I've tried to do is tell the browser that "if" the cookie is set, then alert "you already at the cookie".

    And if the cookie is NOT set, alert "the cookie hasn't been eaten" AND set the cookie afterwards.

    What am I doing wrong?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
     <head>
      <title> eating cookies </title>
      <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
      <script type="text/javascript" src="jquery.cookie.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function($) {
       if ($.cookie('register') == 'yes') { 
          alert("you already ate the cookie");
       }
       else  {
       if ($.cookie('register') != 'yes') { 
          alert("cookie hasn't been eaten");
    	  $.cookie('register', 'yes');
       }
    })
    </script>
     </head>
    
     <body>
      
    
    blah blah blah
    
    
    
    
    
    
     </body>
    </html>


    Also, here's a demo page: http://www.eight7teen.com/DD/cookie-...okie-test.html
    Last edited by TheJoshMan; 10-27-2008 at 03:12 AM. Reason: fixed typo
    --------------------------------------------------
    Reviews, Interviews, Tutorials, and STUFF
    --------------------------------------------------
    Home of the SexyBookmarks WordPress plugin

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Calling external javascript should use src as the attribute and not href:
    Code:
    <script type="text/javascript" href="jquery-1.2.6.min.js"></script>
      <script type="text/javascript" href="jquery.cookie.js"></script>
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. The Following User Says Thank You to rangana For This Useful Post:

    TheJoshMan (10-27-2008)

  4. #3
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You also had an error in your syntax. You miss to close your function:
    Code:
    jQuery(document).ready(function($) {
       if ($.cookie('register') == 'yes') { 
          alert("you already ate the cookie");
       }
       else  {
       if ($.cookie('register') != 'yes') { 
          alert("cookie hasn't been eaten");
    	  $.cookie('register', 'yes');
       }
    }})
    Alternatively, this line:
    Code:
    jQuery(document).ready(function($)
    ...could be changed to:
    Code:
    $(document).ready(function()
    Hope that helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. The Following User Says Thank You to rangana For This Useful Post:

    TheJoshMan (10-27-2008)

  6. #4
    Join Date
    Jan 2006
    Location
    Ft. Smith, AR
    Posts
    795
    Thanks
    57
    Thanked 129 Times in 116 Posts

    Default

    THANKS! That worked perfectly!

    Now, GO EAT COOKIES: http://www.eight7teen.com/cookie-test/cookie-test.html
    --------------------------------------------------
    Reviews, Interviews, Tutorials, and STUFF
    --------------------------------------------------
    Home of the SexyBookmarks WordPress plugin

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
  •