#1
  1. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265

    javascript - cookie based redirect... not working?


    Hi everyone,

    Working on something *seemingly* simply here...

    I want to have a 'flash' intro page on our intranet.

    I only want this page to be viewed once/day by each person.

    I am writing a cookie w/ the name 'seen_flash', and today's Date() as its value

    If the cookie exists, the check its value against today's Date.
    If today is > the cookie date, then it is 'tomorrow', so show the movie
    If today is == the cookie date, then it is still today, so redirect to the homepage

    If the cookie isn't set, then set a new cookie, and play the flash movie

    here's the code:


    <script language="JavaScript">

    function readCookie(name)
    {
    var cookieValue = "";
    var search = name + "=";
    if(document.cookie.length > 0)
    {
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    {
    offset += search.length;
    end = document.cookie.indexOf(";", offset);
    if (end == -1) end = document.cookie.length;
    cookieValue = unescape(document.cookie.substring(offset, end))
    }
    }
    return cookieValue;
    }

    function writeCookie(name, value, hours)
    {
    var expire = "";

    if(hours != null)
    {
    var hours = 25
    expire = new Date((new Date()).getTime() + hours * 1000000);
    expire = "; expires=" + expire.toGMTString();
    }
    document.cookie = name + "=" + escape(value) + expire;
    }

    // set todays date... this is used in several of the enclused function equations
    var today_is = new Date();

    var available_cookies = document.cookie;
    var has_flash_cookie = available_cookies.indexOf("seen_flash");



    if(has_flash_cookie != -1)
    {
    // the cookie is there... read the value. if its still 'today', then skip the flash page... if the cookie is expired, reset, play flash
    var setvalue = readCookie("seen_flash");
    var setvalue_makedate = new Date(setvalue);
    var setvalue_month = 1 + (setvalue_makedate.getMonth()); // months are 0 - 11; add 1 to get actual integer
    var setvalue_date = setvalue_makedate.getDate();
    var s_setvalue_month = setvalue_month.toString();
    var s_setvalue_date = setvalue_date.toString();
    var cookie_date = s_setvalue_month + s_setvalue_date;

    var todays_month = 1 + (today_is.getMonth()); // months are 0 - 11; adding 1 gives you the actual month #
    var todays_day = today_is.getDate();
    var s_todays_month = todays_month.toString();
    var s_todays_day = todays_day.toString();

    var comparison_date = s_todays_month + s_todays_day;

    if(cookie_date == comparison_date)
    {
    // the flash has already been seen; so, redirect
    location.replace('Http://someserver.com');
    } else
    {
    writeCookie("seen_flash", today_is, 25);
    }
    } else if(has_flash_cookie == -1)
    {
    writeCookie("seen_flash", today_is, 25);
    }
    </script>






    Issue: regardless of the cookie state - even if its being read correctly (which is *seems* to be ) ... it always hits the redirect code, never the 'see flash movie' code... also, even if the cookie *isn't* set, it seems to be getting to the redirect code.


    Any thoughts? Where did I go wrong here ?
  2. #2
  3. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265

    no responses?


    hey now...

    bummed that nobody responded to this...

    anyhow, i figured it out, so its not a big deal... but c'mon now, what's up w/ nobody givin me any love?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    an hour away from L.A., California
    Posts
    132
    Rep Power
    13
    {{love love love}} there y'go, hehe.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    an hour away from L.A., California
    Posts
    132
    Rep Power
    13
    I wonder if this script would work for what I am doing (in the post right above yours?)
  8. #5
  9. <blah>
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Melbourne, Australia
    Posts
    483
    Rep Power
    33

    Question How did you fix it?


    Hey Dr !

    Im trying to acheieve the exact same. How did you fix you're above script??

IMN logo majestic logo threadwatch logo seochat tools logo