#1
  1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221

    Is this the right way to check and see if the cookie doesn't exist, create it?


    Hi;

    Is this the right way to check and see if the cookie doesn't exist, create it... and if it exists, get the age?

    (Also how do you style JS in Devshed so it's colored?)

    Code:
    if (getCookie('plt_evergreen_page_load')) {
        console.log('Cookie Exists and the value is:');
        console.log(getCookie('plt_evergreen_page_load'));
        var age = cookie_age();
        console.log('Cookie age is ' + age);
    } else {
        var now = new Date();
        create_time = now.getTime();
        setCookie('plt_evergreen_page_load', create_time);
    }
    
    
    function setCookie(key, value) {
        document.cookie = key + '=' + value + ';expires=' + create_time;
    }
    
    function getCookie(key) {
        var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
        return keyValue ? keyValue[2] : null;
    }
    
    function cookie_age() {
        var now = new Date();
        var age = (now.getTime() - getCookie('plt_evergreen_page_load')) / 1000;
        return cookie_age = Math.round(age) + ' seconds';
    
    }
    https://jsfiddle.net/atv673ke/2/
    Last edited by English Breakfast Tea; July 28th, 2018 at 04:38 AM.
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,904
    Rep Power
    9646
    You can't get the age of a cookie, except by doing something like what you're doing (that is, storing the age itself in a cookie and retrieving its value). Otherwise yes, there is no nice way to get or set cookies so without a library you're stuck reading from and writing to document.cookie.

    Though
    1) Don't call it "create_time" if you're using it as an expiration. I mean, I get why you are, it's just confusing to see.
    2) Don't make setCookie, which is quite usable as a generic function (as is getCookie), have to get expiration from a global variable. Pass it as an argument.

    For the fancy syntax highlighting you need BBCode. It's [highlight=language]...[/highlight].
    javascript Code:
    if (getCookie('plt_evergreen_page_load')) {
        console.log('Cookie Exists and the value is:');
        console.log(getCookie('plt_evergreen_page_load'));
        var age = cookie_age();
        console.log('Cookie age is ' + age);
    } else {
        var now = new Date();
        create_time = now.getTime();
        setCookie('plt_evergreen_page_load', create_time);
    }
     
     
    function setCookie(key, value) {
        document.cookie = key + '=' + value + ';expires=' + create_time;
    }
     
    function getCookie(key) {
        var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
        return keyValue ? keyValue[2] : null;
    }
     
    function cookie_age() {
        var now = new Date();
        var age = (now.getTime() - getCookie('plt_evergreen_page_load')) / 1000;
        return cookie_age = Math.round(age) + ' seconds';
     
    }
  4. #3
  5. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Doesn't create cookies on Safari!

    See this plz

    Live Demo

    Thoughts?
    Last edited by English Breakfast Tea; July 30th, 2018 at 07:21 AM.
  6. #4
  7. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,904
    Rep Power
    9646
    Look at it.
    Code:
    console.log(getCookie(value));
  8. #5
  9. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Originally Posted by requinix
    Look at it.
    Code:
    console.log(getCookie(value));
    Good catch. Changed it to key but same thing.

    It looks like a common problem with Safari.

    Needs a hack, unfortunately.

IMN logo majestic logo threadwatch logo seochat tools logo