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 9 of 9
  1. #1
    New Coder
    Join Date
    Jul 2005
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Cookie Help

    Ok here's my code

    Code:
    <html> 
    
    <script>
    <!--
    var caution = false
    
    function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "Todd" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "")
    if (!caution || (name + "=" + escape(value)).length <= 4000)
    document.cookie = curCookie
    else
    if (confirm("Cookie exceeds 4KB and will be cut!"))
    document.cookie = curCookie
    }
    
    function getCookie(name) {
    var prefix = name + "Todd"
    var cookieStartIndex = document.cookie.indexOf(prefix)
    if (cookieStartIndex == -1)
    return null
    var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
    if (cookieEndIndex == -1)
    cookieEndIndex = document.cookie.length
    return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
    }
    
    
    function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
    document.cookie = name + "Todd" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT"
    }
    }
    
    function fixDate(date) {
    var base = new Date(0)
    var skew = base.getTime()
    if (skew > 0)
    date.setTime(date.getTime() - skew)
    }
    
    var now = new Date()
    fixDate(now)
    now.setTime(now.getTime() + 31 * 24 * 60 * 60 * 1000)
    var name = getCookie("name")
    if (!name)
    name = prompt("Please enter your favorite color:", "")
    setCookie("color", name, now)
    document.write("<body bgcolor=+ color + >")
    //-->
    </script>
    
    
    
    </html>
    now i cant get it to work! any help?!?!?!

  • #2
    Registered User
    Join Date
    Oct 2004
    Posts
    592
    Thanks
    0
    Thanked 1 Time in 1 Post
    yeah, what do you wanna do with that ?

  • #3
    New Coder
    Join Date
    Jul 2005
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code deleted. The constant harrassment here isn't worth my time.
    Last edited by Johnny Lang; 08-12-2005 at 10:42 PM.

  • #4
    New Coder
    Join Date
    Jul 2005
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok....how to i make them do stuff?????? im not that good in java =(

  • #5
    New Coder
    Join Date
    Jul 2005
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Johnny Lang
    Code deleted. The constant harrassment here isn't worth my time.

    WHAT?????!!!!! harrassment?!?!?!

  • #6
    Registered User
    Join Date
    Oct 2004
    Posts
    592
    Thanks
    0
    Thanked 1 Time in 1 Post
    Lawn Gnome, now I understand what you want with your code :

    Then here's what you get :


    Code:
    <SCRIPT LANGUAGE = "JavaScript">
    <!-- Original:  Mattias Sjoberg -->
    /*
    Notes added by icebergz(http://dear.to/icebergz)
    
    This script works in all major browsers; IE, Mozilla and Opera.
    Bg color is just an example, you can extend the script use such as visitor name,bookmark let , popup once and many more.
    
    Nice one!
    
    */
    <!-- Begin
    var expDays = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
    function color(){
    var favColor = GetCookie('color');
    if (favColor == null) {
    favColor = prompt("What is your favorite background color?");
    SetCookie('color', favColor, exp);
    }
    document.bgColor=favColor;
    return favColor;
    }
    function set(){
    favColor = prompt("What is your favorite background color?");
    SetCookie ('color', favColor, exp);
    }
    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();
    }
    // End -->
    </SCRIPT>
    
    </HEAD>
    
    
    <center><BR><BR><BR>
    <SCRIPT>
    document.write('your favorite background color is : ' + color());
    </SCRIPT>
    <br>
    <a href="JavaScript:set()">Change background color</a>
    </center>

  • #7
    New Coder
    Join Date
    Jul 2005
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks but u need to add a auto.refresh somewhere and i dont know where, becuse u have to refresh the page after u chang the color.
    but awsome script

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Just add

    document.bgColor=favColor;

    to the end of function set

    function set(){
    favColor = prompt("What is your favorite background color?");
    SetCookie ('color', favColor, exp);

    document.bgColor=favColor;

    }
    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.

  • #9
    New Coder
    Join Date
    Jul 2005
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    k thanks it works great


  •  

    Posting Permissions

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