1

I am trying to set a cookie using the following code using jquery and qtip. As you can see, when this 'beforeShow' event is triggered I need to set a cookie. I can confirm the event is being triggered as I see the alert although I'm not able to see the cookie being set? Should this code work or am I missing something?
Many thanks,

        api: {
            beforeShow: function() {
                if(document.getElementById('CheckMessage')) {
                    alert("");
                    $.cookie("MyTestExampleCookie", "1");
                    return false;
                }
            }
        },
James Radford
  • 1,815
  • 4
  • 25
  • 40
  • its just a simple cookie to know whether this event has been fired or not so that we can avoid the popup being shown again after the cookie has been set. Can I do it without a plugin? – James Radford Aug 02 '12 at 11:20
  • 1
    You can in raw javascript but jQuery does not have cookie manipulation built in. – Joe Aug 02 '12 at 11:22

3 Answers3

3

Did you install jQuery cookie?

https://github.com/carhartl/jquery-cookie

Johan Haest
  • 4,391
  • 28
  • 37
0

You don't need a cookie unless you need to remember the value after the user has navigated away from the page. If you just want to know if a particular event has been triggered, set a normal variable.

If you do need to use cookies and manipulate these client-side, it is easier with a plugin as suggested above (but not absolutely necessary).

Flash
  • 15,945
  • 13
  • 70
  • 98
  • correct, the user can navigate away, hence the need for a cookie – James Radford Aug 02 '12 at 11:26
  • See http://stackoverflow.com/questions/1458724/how-to-set-unset-cookie-with-jquery, second answer for a non-plugin solution. Its not particularly clean imo. – Flash Aug 02 '12 at 11:27
  • im using this code although I can't see the cookie being set? document.cookie = 'MyTestCookie=Agreed; expires=; path=/' – James Radford Aug 02 '12 at 11:48
  • I'm using the WebDeveloper addon for Firefox and using the view cookie information toolbar option. can see plenty of cookies, just not MyTestCookie – James Radford Aug 02 '12 at 11:48
0
document.cookie = ("MyTestExampleCookie=1");

though it's more advisable to use some cookie lib

Roest
  • 826
  • 6
  • 16