Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookies

Status
Not open for further replies.

Whiteybags

Technical User
May 18, 2004
18
IE
Hey,
I have four files. I am trying to get my cookies to work. I want the name of the course (which you can choose in coursepage.htm to be stored and shown in datapage.htm.
The other two files are .js files. One is just extra data and the other is the cookie code.
It is something very small that I am doing wrong. I was able to get it to store the course code but that is not what I need.
Thanks.


coursepage.htm


<html>

<script type = "text/javascript"
language = "JavaScript 1.5"
src = "pd-cookie-functions.js">
</script>

<script type = "text/javascript"
language = "JavaScript 1.5"
src = "coursedata.js">
</script>

<script>
cookiename = "OnDemandSetUp";
function submitCookie()
{
expires = new Date ();
expires.setMinutes (expires.getMinutes()+1);
SetCookie (cookiename, course[document.form1.course.value], expires);
location = "./datapage.htm";
}
</script>


<form name = form1
action = "Javascript: submitCookie()">

On Demand Set Up
<select name = coursecode onchange = "select()">
<script>
for (var i = 1; i<=10 ; i++)
{
document.write ("<option value = " + i + ">" + coursecode + "</option>");
}
</script>
</select> <br>
<br>

<script>
function select ()
{
var selection = document.form1.coursecode.value;
document.form1.course.value = course[selection];
}
</script>

Course: <input name="course" type="text" id="course"> <br>
<br>

<input type = "submit"> <br>
<br>

<a href = datapage.htm> Back to Data Page </a>

</form>

</html>


datapage.htm


<html>
<head>

<script type = "text/javascript"
language = "JavaScript 1.5"
src = "pd-cookie-functions.js">
</script>

<script>
var cookiename1="OnDemandSetUp";

function GetCourse()
{
var course = GetCookie(cookiename1);
if(course)
document.write ("<b>"+course+"</b>");
else
document.write("[not set]");
}

</script>
</head>

<body>

On Demand Set Up

<br>
<br>
<br>

<script>
GetCourse()
</script>
<br>
<br>

<a href = ./coursepage.htm>Set</a>

</body>

</html>


coursedata.js


var coursecode = new Array();
coursecode [1] = "Choose a Course.";
coursecode [2] = "CourseCode1";
coursecode [3] = "CourseCode2";
coursecode [4] = "CourseCode3";
coursecode [5] = "CourseCode4";
coursecode [6] = "CourseCode5";
coursecode [7] = "CourseCode6";
coursecode [8] = "CourseCode7";
coursecode [9] = "CourseCode8";
coursecode [10] = "CourseCode9";

var course = new Array();
course [1] = "Choose a Course.";
course [2] = "Course1";
course [3] = "Course2";
course [4] = "Course3";
course [5] = "Course4";
course [6] = "Course5";
course [7] = "Course6";
course [8] = "Course7";
course [9] = "Course8";
course [10] = "Course9";


pd-cookie-functions.js


function SetCookie (name,value,expires,path,domain,secure){

var temp = name + "=" + escape (value);
if (expires){
temp += "; expires =" + expires.toGMTString();
}
if (path){
temp += "; path=" + path;
}
if (domain){
temp += "; domain=" + domain;
}
if (secure){
temp += "; secure";
}
document.cookie = temp;
}

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 getCookieVal(offset){
var endstr = document.cookie.indexOf(";", offset);
if (endstr == -1){
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset,endstr));
}
function DeleteCookie (name,path,domain) {
if (GetCookie(name)){
var temp = name + "=";
temp += ((path) ? "; path =" + path : "");
temp += ((domain) ? "; domain =" + domain : "");
temp += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
document.cookie = temp;
}
}

function daysToMS(days){
return days * 24 * 60 * 60 * 1000;
}

function weeksToMS(weeks){
return weeks * 7 * 24 * 60 * 60 * 1000;
}

function yearsToMS(years){
return years *365.25 * 24 * 60 * 60 * 1000;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top