Results 1 to 3 of 3

Thread: Create What's New page redirect returning visitor with cookie.

  1. #1
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create What's New page redirect returning visitor with cookie.

    Is this possible, I see redirect for first time visitor which is also cool, but not what I'm looking for. Thanks.

  2. #2
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    When they first arrive at your site, you need to check whether they already have a cookie set on their browser. If they do; redirect them to a different page or display a div with the newest stuff in it. If they don't, create the cookie...

    Which bits would you need help with?

  3. #3
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Try:
    Code:
    <html>
    <head><title>send cookie</title>
    
    <script type='text/javascript'>
    
    var expdate = new Date();expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
    
    function getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset); if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr)); }
    
    function GetCookie (name) { var arg = name + "="; var alen = arg.length;
    var clen = document.cookie.length; var i = 0; while (i < clen) {
    var j = i + alen; if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; } return null; }
    
    function SetCookie (name, value) { 
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (2 < argc) ? argv[2] : null;
    
    var path = (3 < argc) ? argv[3] : null;
    var domain = (4 < argc) ? argv[4] : null;
    var secure = (5 < argc) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : ""); }
    
    
    function DeleteCookie (name,path,domain) {
    if (GetCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thur, 01-Jan-70 00:00:01 GMT";
    }
    }
    
    function set_href_cookie(href_val){
    
    SetCookie('href_location', href_val, expdate, '/', null, false);
    document.location.href=href_val;
    }
    
    </script>
    
    </head>
    
    <body onbeforeunload="set_href_cookie('href_value')">
    
    <script type='text/javascript'>
    
    cookie_name = GetCookie("href_location");
    
    if(cookie_name){
    alert("Cookie found, redirecting to stored cookie.");
    document.location.href=cookie_name;
    }
    
    </script>
    
    </body>
    </html>
    The only problem here however, is triggering setting the cookie. At the moment, I am using onbeforeunload to trigger it, so it triggers the cookie before they are about to leave the page (don't ask me how it works), however the only problem with this, is that it will trigger it when they are are about to leave any page on your website, However I have made it so that it overwrites the cookie each time, so that when they leave the page for good, it should read the cookie with the URL of the last page they were on.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •