Home › Forums › JavaScript › Cookie activated jQuery
- This topic is empty.
-
AuthorPosts
-
March 22, 2011 at 11:03 am #32066infocentreMember
Hi guys!
I want to run a series of jQuery events that when a user visits the site for the first time.
To do this i am using basic jQuery calls and the jQuery Cookie plugin.
This is the code i have:
But it does not seem to be working... I can't even see the cookie in the cookie list for the site on FF.
Any help or direction on this will be appreciated!
Thanks
March 22, 2011 at 11:39 am #54640infocentreMemberUPDATED CODE:
This code does very nearly what i want it to. But the animations run regardless of the cookies value.
It starts by making a cookie called ‘visited’ with the value of ‘no’, then runs the script and updates the cookies value to ‘yes’.
But if i then refresh the page the animations run again, even though the cookies value is set to ‘yes’….?!
March 22, 2011 at 12:07 pm #54625JohnnybMemberIt looks likes you’re setting the cookie value to be ‘no’ on the very first line everytime, so every time the page is refreshed the cookie is set to ‘no’ and the animation runs everytime. To check this, have you tried console.log(‘visited’); on the line before the if statement to see what the cookie value is right before the animation runs?
March 22, 2011 at 1:55 pm #54621infocentreMember@johnnyb – I think you are spot on! Makes sense when i look at it.
So what really need to do is say ;
If the cookie = ‘yes’, do nothing
Else, run the animations and set the cookie to ‘yes’.How would that translate in code?
Cheers!
March 22, 2011 at 3:03 pm #54617JohnnybMemberWell you could do it the exact way you just described:
var visited = jQuery.cookie('visited');
if (visited == 'yes') {
...dont run animations... return false;
} else {
... run animations!...
jQuery.cookie('visited', 'yes'); //then set cookie at end of animation
}
OR, you could just check to see whether the cookie is undefined:
var visited = jQuery.cookie('visited'); //if first visit then this should come back as undefined.
if (visited == 'undefined') {
...run animations...
jQuery.cookie('visited', 'yes'); //sets cookie at end of animation
}
Haven’t tested either but they should work.
March 23, 2011 at 6:45 am #54470infocentreMember@jhonnyb
Thanks for your help mate – the completed code is below!
March 23, 2011 at 9:16 am #54417JohnnybMemberAwesome, no problemo. Does it work ok?
March 23, 2011 at 11:38 am #54425infocentreMemberMarch 23, 2011 at 2:31 pm #54435JohnnybMemberSweet, that works really nice!
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.