JavaScript, Variables, and Cookies
Results 1 to 3 of 3

Thread: JavaScript, Variables, and Cookies

  1. #1
    Registered User
    Join Date
    Aug 2000
    Location
    Middle of nowhere
    Posts
    473

    Unhappy JavaScript, Variables, and Cookies

    I have a page that is attempting to determine your bandwidth to my site. I have the java functions in place to do so . But when I try and put the resulting value in a cookie it continually says "undefined" in the cookie where the value should be. I need to pass this variable to the next page to display it. Here is my code, someone tell me what I am doing wrong. Keep in mind I don't have much experience in Java.

    There are no <> because for some reason anything between them won't post here.
    Code:
    script language = "JavaScript"
    cookie_name = "Results";
    var kbytes;
    var kbits;
    
    function RightNow() {
    time = new Date();
    return time.getTime();
    }
    
    function CalculateSpeed(timeStart) {
    timeEnd = RightNow();
    timeElapsed = (timeEnd - time Start)/1000;
    kbytes = 200/timeElapsed;
    kbits = kbytes * 1024 * 8;
    }
    
    function puCookie() {
    if (document.cookie !=document.cookie)
    {index = document.cookie.index0f(cookie_name);}
    else
    {index = -1;}
    if (index == -1)
    {
    TestResults = kbits
    document.cookie=cookie_name + "=" + TestResults + "; expires=Monday, 04-Apr-2010 05:00:00 GMT";
    }
    }
    
    /script
    /head
    
    body
    script language="JavaScript"
    timeStart = RightNow();
    /script
    
    img src="200.jpg" width="20" onload=CalculateSpeed(timeStart);"
    
    script language="JavaScript"
    putCookie()
    nextpage='results.asp'
    document.locations.href=nextpage;
    /script
    
    /body
    /html
    Last edited by CJK; January 10th, 2003 at 04:55 PM.
    To each his/her own.

  2. #2
    Registered User ScottieM3's Avatar
    Join Date
    Mar 2001
    Location
    NJ
    Posts
    202

    Smile

    hey whats going on CJK,
    i've been on a 10 month break so my JavaScript might be a little rusty

    Below I have pasted a working version of your script just copy and paste the code.
    <span style="color: 0000FF;">
    &lt;script language="JavaScript"&gt;
    cookie_name = "Results";
    var kbytes;
    var kbits;

    function RightNow() {
    &nbsp;&nbsp;time = new Date();
    &nbsp;&nbsp;return time.getTime();
    &nbsp;&nbsp;}

    var timeStart = RightNow();

    function CalculateSpeed() {
    &nbsp;&nbsp;timeEnd = RightNow();
    &nbsp;&nbsp;timeElapsed = (timeEnd - timeStart) / 1000;
    &nbsp;&nbsp;kbytes = 200 / timeElapsed;
    &nbsp;&nbsp;kbits = kbytes * 1024 * 8;
    &nbsp;&nbsp;putCookie();
    &nbsp;&nbsp;}

    function putCookie() {
    &nbsp;&nbsp;if (document.cookie.indexOf(cookie_name) == -1) {
    &nbsp;&nbsp;&nbsp;&nbsp;document.cookie = cookie_name + "=" + kbits + "; expires=Monday, 04-Apr-2010 05:00:00 GMT";
    &nbsp;&nbsp;&nbsp;&nbsp;}

    &nbsp;&nbsp;document.location.href = "results.asp";

    &nbsp;&nbsp;}

    &lt;/script&gt;

    &lt;/head&gt;

    &lt;body&gt;

    &lt;img src="200.jpg" border="0" onload="CalculateSpeed();"&gt;
    </span>
    <br>
    A quick comment: instead of using a cookie to pass the value.. why not use a query string and parse the query sting on the next page i.e.
    <br>
    <span style="color: 0000FF;">
    &lt;script language="JavaScript"&gt;

    cookie_name = "Results";
    var kbytes;
    var kbits;

    function RightNow() {
    &nbsp;&nbsp;time = new Date();
    &nbsp;&nbsp;return time.getTime();
    &nbsp;&nbsp;}

    var timeStart = RightNow();

    function CalculateSpeed() {
    &nbsp;&nbsp;timeEnd = RightNow();
    &nbsp;&nbsp;timeElapsed = (timeEnd - timeStart) / 1000;
    &nbsp;&nbsp;kbytes = 200 / timeElapsed;
    &nbsp;&nbsp;kbits = kbytes * 1024 * 8;

    &nbsp;&nbsp;<span style="color: FF3333;">document.location.href = "results.asp?value=" + kbits;</span>

    &nbsp;&nbsp;}

    &lt;/script&gt;

    &lt;/head&gt;

    &lt;body&gt;

    &lt;img src="200.jpg" border="0" onload="CalculateSpeed();"&gt;
    </span>
    <br>
    with this script you can parse the query string via JavaScript or ASP sinse I see your next page is an asp page.
    Just a suggestion


    Let me know if this works for you
    Last edited by ScottieM3; January 12th, 2003 at 10:33 AM.
    Spend One Hour of Every Day Like it was Your last, you'll Live much better that way

  3. #3
    Registered User
    Join Date
    Aug 2000
    Location
    Middle of nowhere
    Posts
    473
    Thank you for the help. The query string did the trick...dumb cookies. Actually I had run into another problem, for some reason id did not work as I expected so I went a different route, but I did save it just incase I can use it for anything else or for reference. Thanks again!
    To each his/her own.

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
  •