Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Cookie activated jQuery

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #32066
    infocentre
    Member

    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

    #54640
    infocentre
    Member

    UPDATED 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’….?!

    #54625
    Johnnyb
    Member

    It 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?

    #54621
    infocentre
    Member

    @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!

    #54617
    Johnnyb
    Member

    Well 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.

    #54470
    infocentre
    Member

    @jhonnyb

    Thanks for your help mate – the completed code is below!

    #54417
    Johnnyb
    Member

    Awesome, no problemo. Does it work ok?

    #54425
    infocentre
    Member
    #54435
    Johnnyb
    Member

    Sweet, that works really nice!

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.