Can't Seem to Access Variable from readCookie()..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LayneMitch via WebmasterKB.com

    Can't Seem to Access Variable from readCookie()..

    Okay....now can anyone tell me why my 'document.write ' command isn't reading
    the variables assigned to the cookie_info object through the readCookie()
    function? This is really teeing me off.

    Here's my updated code.

    <html><head><ti tle>Problem14</title>

    <style type="text/css">
    #greeting {position:absol ute; left;50px; top:100px;}
    </style>

    <script type="text/javascript">

    function readCookie(the_ info)
    {
    if(document.coo kie)
    {
    var the_cookie = document.cookie ;
    var the_cookie = unescape(the_co okie);

    var broken_cookie = the_cookie.spli t("=");

    var the_values = broken_cookie[1];
    var separated_value s = the_values.spli t("/");

    var broken_info;
    var property_value= "";//once you split the backslash you need loop
    through
    // the values and store them in a variable so you
    // can reference them later in the code

    for (var i=0; i<separated_val ues.length; i++)
    {
    var property_value= separated_value s[i];
    var broken_info = property_value. split(":");
    var the_property = broken_info[0];
    var the_values = broken_info[1];
    the_info[the_property] = the_values;

    }
    }

    // Return the info you got passed
    return the_info;
    }

    function setCookie(){

    //Set Up the Date String

    var today = new Date();
    var the_month = today.getMonth( );
    var the_day = today.getDate() +2;
    var the_year = today.getFullYe ar();
    var mth_list = new Array("January" ,"February","Ma rch","April","M ay","June",
    "July",
    "August","Septe mber","October" ,"November","De cember");
    var alpha_mth = mth_list[the_month];
    var condensed_date = alpha_mth+" "+the_day+","+t he_year;

    //Set Up the Time String

    var the_time = today.getTime() ;
    var the_secs = today.getSecond s();
    var the_time = Math.floor(the_ time/60);
    var the_mins = toSt(the_time%6 0);
    var the_time = Math.floor(the_ time/60);
    var the_hours = toSt(the_time%2 4);
    var the_time_string = the_hours+":"+t he_mins+":"+the _secs;

    //Set Up The Cookie

    var the_cookie = "date:condensed _date/time:the_time_s tring";

    //above...forcing it to read the variable..

    document.cookie = "my_cookie="+es cape(the_cookie );

    //Call The Display Function

    displayDateTime (condensed_date ,the_time_strin g);

    }

    function toSt(n) {
    var s=""
    if (n<10) s+="0"
    return s+n;
    }

    function displayDateTime (date,time){

    var the_div=documen t.getElementByI d("greeting") ;
    var the_message="We lcome. Today's date is "+date+" and the time is "+time;
    var text=document.c reateTextNode(t he_message);
    the_div.appendC hild(text);

    }

    var cookie_info = {};
    cookie_info = readCookie(cook ie_info);

    </script></head>

    <body onLoad=setCooki e();>

    <div id="greeting"> </div>

    <script type="text/javascript">

    var the_name=prompt ("what's your name","");
    alert ("Welcome, "+the_name+ " to my page.");

    document.write( "The last time you were here or the page was updated was on"+
    cookie_info["date"]+" at "+ cookie_info["time"]);
    </script>

    </body>
    </html>

    --
    Message posted via WebmasterKB.com


  • LayneMitch via WebmasterKB.com

    #2
    Re: Can't Seem to Access Variable from readCookie()..

    LayneMitch wrote:
    >Okay....now can anyone tell me why my 'document.write ' command isn't reading
    >the variables assigned to the cookie_info object through the readCookie()
    >function? This is really teeing me off.
    >
    >Here's my updated code.
    >
    ><html><head><t itle>Problem14</title>
    >
    ><style type="text/css">
    >#greeting {position:absol ute; left;50px; top:100px;}
    ></style>
    >
    ><script type="text/javascript">
    >
    >function readCookie(the_ info)
    >{
    if(document.coo kie)
    {
    var the_cookie = document.cookie ;
    var the_cookie = unescape(the_co okie);
    >
    var broken_cookie = the_cookie.spli t("=");
    >
    var the_values = broken_cookie[1];
    var separated_value s = the_values.spli t("/");
    >
    var broken_info;
    var property_value= "";//once you split the backslash you need loop
    >through
    // the values and store them in a variable so you
    // can reference them later in the code
    >
    for (var i=0; i<separated_val ues.length; i++)
    {
    var property_value= separated_value s[i];
    var broken_info = property_value. split(":");
    var the_property = broken_info[0];
    var the_values = broken_info[1];
    the_info[the_property] = the_values;
    >
    }
    }
    >
    // Return the info you got passed
    return the_info;
    >}
    >
    >function setCookie(){
    >
    >//Set Up the Date String
    >
    >var today = new Date();
    >var the_month = today.getMonth( );
    >var the_day = today.getDate() +2;
    >var the_year = today.getFullYe ar();
    >var mth_list = new Array("January" ,"February","Ma rch","April","M ay","June",
    >"July",
    "August","Septe mber","October" ,"November","De cember");
    >var alpha_mth = mth_list[the_month];
    >var condensed_date = alpha_mth+" "+the_day+","+t he_year;
    >
    >//Set Up the Time String
    >
    >var the_time = today.getTime() ;
    >var the_secs = today.getSecond s();
    >var the_time = Math.floor(the_ time/60);
    >var the_mins = toSt(the_time%6 0);
    >var the_time = Math.floor(the_ time/60);
    >var the_hours = toSt(the_time%2 4);
    >var the_time_string = the_hours+":"+t he_mins+":"+the _secs;
    >
    >//Set Up The Cookie
    >
    >var the_cookie = "date:condensed _date/time:the_time_s tring";
    >
    >//above...forcing it to read the variable..
    >
    >document.cooki e = "my_cookie="+es cape(the_cookie );
    >
    >//Call The Display Function
    >
    >displayDateTim e(condensed_dat e,the_time_stri ng);
    >
    >}
    >
    >function toSt(n) {
    >var s=""
    >if (n<10) s+="0"
    >return s+n;
    >}
    >
    >function displayDateTime (date,time){
    >
    >var the_div=documen t.getElementByI d("greeting") ;
    >var the_message="We lcome. Today's date is "+date+" and the time is "+time;
    >var text=document.c reateTextNode(t he_message);
    >the_div.append Child(text);
    >
    >}
    >
    >var cookie_info = {};
    >cookie_info = readCookie(cook ie_info);
    >
    ></script></head>
    >
    ><body onLoad=setCooki e();>
    >
    ><div id="greeting"> </div>
    >
    ><script type="text/javascript">
    >
    >var the_name=prompt ("what's your name","");
    >alert ("Welcome, "+the_name+ " to my page.");
    >
    >document.write ("The last time you were here or the page was updated was on"+
    cookie_info["date"]+" at "+ cookie_info["time"]);
    ></script>
    >
    ></body>
    ></html>
    Had to post this as a separate thread..didn't want it to get lost...need
    resolution soon. Thanks.

    --
    Message posted via WebmasterKB.com


    Comment

    • LayneMitch via WebmasterKB.com

      #3
      Re: Can't Seem to Access Variable from readCookie()..

      Also, can anyone tell me how to get time in the United States with this code?

      --
      Message posted via WebmasterKB.com


      Comment

      • Dr J R Stockton

        #4
        Re: Can't Seem to Access Variable from readCookie()..

        In comp.lang.javas cript message <891b7d7daf037@ uwe>, Sat, 23 Aug 2008
        16:31:06, LayneMitch via WebmasterKB.com <u39402@uwe.?.i nvalidposted:
        >Also, can anyone tell me how to get time in the United States with this code?
        Does that mean that it is you or the time that is to be in the United
        States? From your previous writings, I'd thought that you were there
        already.

        Whatever "this code" may be, then that depends on how it's written and
        perhaps on where you are. It must depend on whereabouts in the United
        States you want, too.

        <http://web.archive.org/web/200702022...ering.com/faq/
        index.htmlsecti on 3.2 could have helped you.

        Google for JavaScript date and time elsewhere and you get 141,000
        pages found; try the first one.

        <URL:http://www.merlyn.demo n.co.uk/js-date5.htm>

        --
        (c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05.
        Web <URL:http://www.merlyn.demo n.co.uk/- w. FAQish topics, links, acronyms
        PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/- see 00index.htm
        Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

        Comment

        Working...