Current location: Hot Scripts Forums » General Web Coding » JavaScript » cookie problems


cookie problems

Reply
  #1 (permalink)  
Old 04-23-06, 11:30 PM
pricecd pricecd is offline
New Member
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
cookie problems

I'm basically trying to make a computer based training module with JS. There are 5 sections and I made it so that a cookie is created through an onload on each page. This cookie is for user tracking and if the user quits out while they are in a section when they return they can go to the section they were at before and continue on the page they were on. Now the problem is the there are links to each part on a home pageand I made the link to each section an onClick retrieve cookie. The only problem I didn't forsee if what is there is no cookie? The page loads as undefined. I was just wondering on how I could set it so that if there is no cookie they go to the first page of the desired section.

here is my index page code:

Code:
<html>
<head>
<title>Untitled Document</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript">

function setCookie(name,data,days)
{
	var cookieEnabled=(navigator.cookieEnabled)? true : false

	days=10;

	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}

	if(typeof navigator.cookieEnabled=="undefined" && !cookieEnabled)
	{ 
		alert('cookies not enabled');
	}
	else
	{
		var cookieName=name;
		document.cookie=cookieName+'='+data+expires;
	}
}


function retCookie(cookieName)
{

	var myCookie;
	var name=cookieName;

	if(document.cookie)
	{
		index=document.cookie.indexOf(name);
		if(index!=-1)
		{
			nameStart = (document.cookie.indexOf("=", index) + 1);
			nameEnd = document.cookie.indexOf(";", index);
			if(nameEnd==-1)
			nameEnd=document.cookie.length;
			myCookie=document.cookie.substring(nameStart,nameEnd);
		
		}

	}
	
	return myCookie;

}	




alert(document.cookie)
</script>	

</head>

<body bgcolor="#CCCCCC">


<center>
<div width:750px class="div">
<table width="750px" height="643px" background="images/bg.gif">

	<tr>
	 
	  <td valign="top" height="53"><img src="images/Untitled-1_01.gif" width="748" height="53">
	</tr>
	<tr>
	  <td width="750" valign="top"><table width="750" height="480" border="0" cellpadding="0" 					cellspacing="0">
        <tr>
          <th width="300" scope="row"><div align="center"><img src="images/computer.gif" width="260" height="275"></div></th>
          <td valign="top"><table width="450" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <th scope="row" class="text"><div align="left">
                <p>                  Welcome to Clark Price's Computer Based Training Module! In this module, you will learn how to properly place several key computer components into your machine.When buying your machine all of your parts are in place and ready for use, but what if you want to upgrade or a piece breaks and needs replaced. Knowing how to fix your own computer is not only a useful skill, but it will also save you from spending money on jobs you could probably just do yourself.Always remember safety before you opening your machine and handling the components inside. Keep away from magnetic areas like carpet and touch metal to ground youself. You begin with any module that you want, but it would probably be best to just start with the first one! <br>
                  <br>
                  <strong>Module 1</strong>: Video Cards</p>
                <p><strong>Module 2</strong>: <a href="javascript: window.open( retCookie('lastPage2'),'fullscreen')">Hard Drives</a></p>
                <p><strong>Module 3</strong>: RAM</p>
              <p><strong>Module 4</strong>: <a href="javascript: window.open( retCookie('lastPage4'),'fullscreen')">Optical Drives</a><br>
                  <br>
                  <strong>Module 5</strong>: Motherboards
                    <br>
                    <br>
                    <br>
                    <br>
                    <br>
                    <br>
                </p>
                <center>
                <br>
                </center>
                    <br>
              </div></th>
            </tr>
          </table></td>
        </tr>
        
      </table></td>
  </tr>
	
	<tr>
		<td width="750" height="55"><img src="images/Untitled-1_04.gif" width="750" height="55"></td>
	</tr>
</table>
</div>
</center>


</body>
</html>
and here is a sample first page of one of my sections:

Code:
<html>
<head>
<title>Untitled Document</title>
<link rel="stylesheet" href="style.css" type="text/css"></head>

<script type="text/javascript">
if(retCookie('name') == "")
{
	document.location=cdrom1.html

}
function setCookie(name,data,days)
{
	var cookieEnabled=(navigator.cookieEnabled)? true : false

	days=10;

	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}

	if(typeof navigator.cookieEnabled=="undefined" && !cookieEnabled)
	{ 
		alert('cookies not enabled');
	}
	else
	{
		var cookieName=name;
		document.cookie=cookieName+'='+data+expires;
	}
}

	
function retCookie(cookieName)
{
	var myCookie;
	var name=cookieName;

	if(document.cookie)
	{
		index=document.cookie.indexOf(name);
		if(index!=-1)
		{
			nameStart = (document.cookie.indexOf("=", index) + 1);
			nameEnd = document.cookie.indexOf(";", index);
			if(nameEnd==-1)
				nameEnd=document.cookie.length;
			myCookie=document.cookie.substring(nameStart,nameEnd);
		
		}

	}
	return myCookie;


}




</script>

	
<body bgcolor="#CCCCCC" onLoad="setCookie('lastPage4',document.location)">


<center>
<div width:750px class="div">
<table width="750px" height="643px" background="images/bg.gif">

	<tr>
	 
	  <td valign="top" height="53"><img src="images/Untitled-1_01.gif" width="748" height="53">
	</tr>
	<tr>
	  <td width="750" valign="top"><table width="750" height="480" border="0" cellpadding="0" 					cellspacing="0">
        <tr>
          <th valign="top"width="300" scope="row" class="text"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <th scope="row"><div align="center">
            <p><br>
                <br>
                    <img src="CD.gif" width="250" height="229"></p>
            <p align="justify"><center></center>
            <div class="fyi">
              <div align="left"><strong>For Your Information:</strong> CD stands for Compact Disk and DVD stand for <strong>Digital Versatile Disc</strong>.</div>
            </div></p>
            <p><br>
            </p>
          </div></th>
            </tr>
          </table></th>
          <td valign="top"><table width="450" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <th scope="row" class="text" valign="top"><p><br>
                    <br>
              <div align="left" >
                <p><strong>Brief Lesson: CD/DVD Rom Drives<br>
                      <br>
                  CD-ROM Drives </strong>- CD-ROM Drives are the medium at which CDs are read. The drives reads CDs by spinning them at a designated speed. The lowest speed being a 1x speed which can read 150 kb/s. The higher the number the higher amount of data can be read and processed per second. CDs once were only used for audio, but are now also used to store data and video. The CD opened up huge possibilities and is vastly superior to the flopp disc. The average CD holds about 700 MB of storage space. Because of this the CD quickly went from being a expensive luxury to an inexpensive storage medium that was widely used. Before long CD-Rewritable drives became availible. Now people were able to store their own files onto discs.  </p>
                <p>The way CD-ROM drives are classified was alluded to earlier. The Drive Speed, the Access Time, and the Data Transfer Speed are all factors.</p>
                <p><strong>Drive Speed</strong> - How fast the drive can rotate a CD.<br>
                  <br>
                  <strong>Access Time</strong> - The average time it takes for a drive to access a particular file on the CD</p>
                <p><strong>Data Transfer Speed</strong> - How much data can be found and sent to the computer in one second.<br>
                  <br>
                  <strong>DVD-ROM Drives
                        - </strong>The DVD is the storage medium that evolved from CDs. DVD-Drives work a lot like CD Drives in how they read the data by spinning the DVD and using a laser to read the data.The DVD is the same dimension as a CD, but can hold several times more data. A dvd can hold up to 4.7 GB of information, slightly less then 7x as much as a CD. DVD Drives not only play DVDs, but they can also play CDs. A person can store up to 8 hours of CD quality music and about 133 minutes of high resolution video. Unlike CDs, DVD can have multiple layers, which can store a lot of extra information.</p>
                <p>Single Sided Single Layered DVD - 4.7 GB<br>
                  Single Sided Double Layered DVD - 8.5 GB<br>
                  Double Sided Double Layered DVD - 17 GB           
                  <strong><br>
                     <br>
                     <br>
                      </strong><br>
                          <br>
                          <br>
                          <br>
                          <br>
                </p>
                </div>
                <center>
                  <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <th scope="row"><div align="center"><img src="images/back.gif" alt="sd" width="88" height="22"></div></th>
                      <td><div align="center"><a href="cdrom2.html"><img src="images/next.gif" alt="sd" width="88" height="22" border="0" onClick="javascript:setCookies()"></a></div></td>
                    </tr>
                  </table>
                  <br>
                </center></th>
              <th scope="row" class="text" width="20px"><div align="left">
                <p><br>
                    <br>
                    <br>
                </p>
                <br>
              </div></th>
            </tr>
            <tr>
              <th scope="row" class="text">&nbsp;</th>
              <th scope="row" class="text">&nbsp;</th>
            </tr>
          </table></td>
        </tr>
        
      </table></td>
  </tr>
	
	<tr>
		<td width="750" height="55"><img src="images/Untitled-1_04.gif" width="750" height="55"></td>
	</tr>
</table>
</div>
</center>


</body>
</html>

Last edited by pricecd; 04-24-06 at 12:10 AM.
Reply With Quote
  #2 (permalink)  
Old 04-24-06, 12:58 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
I don't know how your pages are named, but you could do a simple check at the end to see if myCookie has been set or not. Then from there you might be able to let it enter the first page's name instead. Maybe sending the first page's name as an argument to retCookie would help.

Code:
function retCookie(cookieName,defaultPage)
{
	var myCookie;
	var name=cookieName;

	if(document.cookie)
	{
		index=document.cookie.indexOf(name);
		if(index!=-1)
		{
			nameStart = (document.cookie.indexOf("=", index) + 1);
			nameEnd = document.cookie.indexOf(";", index);
			if(nameEnd==-1)
				nameEnd=document.cookie.length;
			myCookie=document.cookie.substring(nameStart,nameE  nd);
		
		}

	}
	return myCookie||defaultPage; //if myCookie is undefined, false, "", null or 0, the value of defaultPage is returned instead.


}
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
access cookie from different path crmpicco JavaScript 1 02-21-06 03:03 PM
maintaing state with a server Betty Perl 0 12-09-05 07:23 AM
Setting a cooke (time it expires) mcrob PHP 4 04-27-05 12:13 PM
Passing info between pages w/ URL HairySpider JavaScript 6 01-09-05 08:47 AM
Strange Cookie Problems darkreaper PHP 1 12-30-04 06:39 PM


All times are GMT -5. The time now is 11:09 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.