Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Cookie Question

  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Cookie Question

    Can someone help me with a cookie? I have a flash intro page that I only want to be displayed the first time that the page is viewed, other wise I want the visitor to be redirected to a new page or the page that would come after the flash intro.

    I have seen other code on here that does something similar, such as the code below:

    <html>
    <head>
    <title></title>

    <SCRIPT LANGUAGE="JavaScript">
    <!--
    cookie_name = "Cookie1";

    function doCookie()
    {
    gettimes();
    if(document.cookie)
    {
    index = document.cookie.indexOf(cookie_name);
    }
    else
    {
    index = -1;
    }
    var expires = "Monday, 04-Apr-2010 05:00:00 GMT"
    if (index == -1)
    {
    document.cookie=cookie_name+"=1; expires=" + expires;
    }
    else
    {
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    if (countend == -1)
    {
    countend = document.cookie.length;
    }
    count = eval(document.cookie.substring(countbegin, countend)) + 1;
    document.cookie=cookie_name+"="+count+"; expires=" + expires;
    }
    }

    function gettimes()
    {
    if(document.cookie)
    {
    index = document.cookie.indexOf(cookie_name);
    if (index != -1)
    {
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    if (countend == -1)
    {
    countend = document.cookie.length;
    }
    count = document.cookie.substring(countbegin, countend);
    if (count >= 1)
    {
    redirect();
    return null;
    }
    else
    {
    return null;
    }
    }
    }
    }

    function redirect()
    {
    window.location="index2.html";
    }
    //-->
    </SCRIPT>
    </head>

    <body onLoad="doCookie()">

    This is the body of the page.

    </body>
    </html>


    But this code still displays the flash page and then quickly redirects you to the second page. I do not want any of the original or flash page to be displayed at all, I just want you to be forwarded.

    I was thinking that this would work but it does not display what I want in the body of the page:

    <html>
    <head>
    <title></title>

    <SCRIPT LANGUAGE="JavaScript">
    <!--
    cookie_name = "Cookie1";

    function doCookie()
    {
    gettimes();
    if(document.cookie)
    {
    index = document.cookie.indexOf(cookie_name);
    }
    else
    {
    index = -1;
    }
    var expires = "Monday, 04-Apr-2010 05:00:00 GMT"
    if (index == -1)
    {
    document.cookie=cookie_name+"=1; expires=" + expires;
    }
    else
    {
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    if (countend == -1)
    {
    countend = document.cookie.length;
    }
    count = eval(document.cookie.substring(countbegin, countend)) + 1;
    document.cookie=cookie_name+"="+count+"; expires=" + expires;
    }
    }

    function gettimes()
    {
    if(document.cookie)
    {
    index = document.cookie.indexOf(cookie_name);
    if (index != -1)
    {
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    if (countend == -1)
    {
    countend = document.cookie.length;
    }
    count = document.cookie.substring(countbegin, countend);
    if (count >= 1)
    {
    redirect();
    return null;
    }
    else
    {
    showPage();
    return null;
    }
    }
    }
    }

    function redirect()
    {
    window.location="index2.html";
    }

    function showPage()
    {
    document.write("This is the body of the page.");
    }
    //-->
    </SCRIPT>
    </head>

    <body onLoad="doCookie()">


    </body>
    </html>


    Any help is greatly appreciated!

    Thanks.
    -Dave

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    With the script you are using the script is not fired until the document is loaded... Either remove the doCookie function wrapper and accompying function call from the onload event or remove the onload event and add doCookie() immediately prior to the closing script tag...

    .....Willy

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Exactly what I needed, thanks!


  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by PROWEBWORKS.NET
    Exactly what I needed, thanks!

    Cheers... I am pleased to hear you understood...
    But, please tell me... Was it Either or Or...

    .....Willy

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Both actually, I removed the onLoad event and the call to the function and it works perfectly. Here is the code in the header:

    <SCRIPT language=JavaScript>
    <!--
    cookie_name="cookie_name"
    expDays = 1;

    var count = get_cookie('cookie_name');
    if (count == null) {
    count=1;
    var exp = new Date();
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
    set_cookie('Andreottis', count, exp);

    whatever_function()
    }

    function get_cookie (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 get_cookie_val (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
    return null;
    }

    function get_cookie_val(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
    }

    function set_cookie (name, value,expires) {
    var argv = set_cookie.arguments;
    var argc = set_cookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? 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 delete_cookie (name) {
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = get_cookie (name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    }

    Thanks again!


  •  

    Posting Permissions

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