-
January 10th, 2003, 03:46 PM
#1
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.
-
January 11th, 2003, 10:44 AM
#2
Registered User
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;">
<script language="JavaScript">
cookie_name = "Results";
var kbytes;
var kbits;
function RightNow() {
time = new Date();
return time.getTime();
}
var timeStart = RightNow();
function CalculateSpeed() {
timeEnd = RightNow();
timeElapsed = (timeEnd - timeStart) / 1000;
kbytes = 200 / timeElapsed;
kbits = kbytes * 1024 * 8;
putCookie();
}
function putCookie() {
if (document.cookie.indexOf(cookie_name) == -1) {
document.cookie = cookie_name + "=" + kbits + "; expires=Monday, 04-Apr-2010 05:00:00 GMT";
}
document.location.href = "results.asp";
}
</script>
</head>
<body>
<img src="200.jpg" border="0" onload="CalculateSpeed();">
</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;">
<script language="JavaScript">
cookie_name = "Results";
var kbytes;
var kbits;
function RightNow() {
time = new Date();
return time.getTime();
}
var timeStart = RightNow();
function CalculateSpeed() {
timeEnd = RightNow();
timeElapsed = (timeEnd - timeStart) / 1000;
kbytes = 200 / timeElapsed;
kbits = kbytes * 1024 * 8;
<span style="color: FF3333;">document.location.href = "results.asp?value=" + kbits;</span>
}
</script>
</head>
<body>
<img src="200.jpg" border="0" onload="CalculateSpeed();">
</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
-
January 16th, 2003, 12:44 PM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|
Bookmarks