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 8 of 8
  1. #1
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Smile Cookies Cookies and well, cookies!

    Hi guys, does anyone know how to make a page set a cookie (with a value; yes, no or mby) automatically and then redirect the page to another once it has done so?

    Can fix your Chopstick!
    *Dr.Wong!

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Yes
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Question Okay, uh, mind telling me how?

    Me say cool! You wanna let the secret out?

    (please, I kinda need it!)

    *Wong

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Well you did only ask if anyone knew how to do it


    <script language=javascript>
    <!--
    days=365 // number of cookie days
    cookie_name="redirect"

    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 chk_cookie() {
    var count = get_cookie(cookie_name)

    if (count == null) {
    count=1
    var exp = new Date()
    exp.setTime(exp.getTime() + (days*24*60*60*1000))
    set_cookie(cookie_name,count, exp)
    do_this()
    }
    else{}

    }

    function do_this(){
    window.location="yourpage.htm"
    }

    chk_cookie()

    // -->
    </script>
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Smile

    Well I thought it was a little impolite to just say, HEY YOU! TELL ME HOW TO DO THIS!

    Anyways, thanks for the script.

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    I have to admit to being a bit puzzled with your request.

    You link to a page that sets a cookie then immediately get redirected to another page.

    Why not link straight to the other page and have the cookie set in that page.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #7
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    You see the options of the page that the cookie page rediects to is determined by the cookie that is set on the cokie page, clicking a different link on the 'index' page will take you to a different cookie page.

    Ultimately I would like a button that could set a cookie and be a link to another page!

    But I have asked before and got no reply.

    If you know how to do this, please let me know

    Can Fix your Chopstick!
    *DR.Wong

  • #8
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Lightbulb Have a look...

    This is one of the pages that I use to set cookies;
    Now what I want is when you click on the setcookie button, it must set the cookie and go to lets say,'page2.htm' .

    The Setcookie button can be found at the bottom of the code.

    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    var expDays = 365;
    var exp = new Date();
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

    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 = (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 DeleteCookie (name) {
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = GetCookie (name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    }

    var favorite = GetCookie('examplecookie');

    if (favorite != null) {
    switch (favorite) {
    case 'yes' : url = 'page1.htm'; // change these!
    break;
    case 'no' : url = 'page2.htm';
    break;
    case 'prompt' : url = 'page3.htm';
    break;


    }
    window.location.href = url;
    }
    // End -->
    </script>
    </head>
    <body>

    <center><font face=verdana size=5 color=black>My Cookie Page</font><br>
    <hr>
    <p align=left><font face=verdana size=5 color=Black>Example Cookies</font>
    <form>
    <p align=left><table><tr><td>
    <input type=button name="prompt" value=" prompt " onClick="SetCookie('examplecookie', this.name, exp);"><br><br>

    <input type=button name="yes" value=" yes " onClick="SetCookie('examplecookie', this.name, exp);"><br><br>
    <input type=button name="no" value=" no " onClick="SetCookie('examplecookie', this.name, exp);">




    </td></tr>
    </table>
    </form>
    </p>
    </body>
    </html>


    I have also put in the cookie checker code (located above buttons) just incase you need it for some reason.

    Can Fix Your Chopstick!
    *DR.Wong


  •  

    Posting Permissions

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