Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become an Expert!

Join 244,261 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,307 people online right now. Registration is fast and FREE... Join Now!




cookies problem

 
Reply to this topicStart new topic

cookies problem

pricecd
21 Apr, 2006 - 03:16 PM
Post #1

New D.I.C Head
*

Joined: 21 Apr, 2006
Posts: 2


My Contributions
I'm basically trying to make a computer based training module. We had to choose a topic and basically make a module that is to educate someone on the topic. At the end of the section there will be a quiz top test you on what you learn. We need to have an index or beginning page that lists all of the sections out so that the user can start wherever they want. I have all of my data done, but now I need to start the javascript. I wants it so that when you enter a section the page the user is on should be tracked. If for some reason the user closes the browser, he or she should be able to start on the page of the section they were on when the browser closed. For example if they were on the third page of the section when the browser was closed they would go back to the module later choose the section they were reading and it would remember and load the third page.

I created this set and retrieve function on each page of the module:

CODE

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;



and this in the bodytag of each page

CODE

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


I've only used this one of of the module links so far:

CODE

       <p><strong>Module 4</strong>: <a href="javascript: window.open( retCookie('lastPage'),'fullscreen')">Optical Drives</a><br>


I didn't think about what will happen if they close the browser come back start a whole different section. the cookie from before will interfere and it'll send them to the wrong place. Is there anyway I can adjust this so that if you want to start a new section the cookie from before won't interfere?

User is offlineProfile CardPM
+Quote Post


William_Wilson
RE: Cookies Problem
21 Apr, 2006 - 09:40 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,446



Thanked: 55 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
when you refresh or close and open a browser, temporary cookies can be destroyed (like the ones yours may be)

I believe most of your problem is because when your form loads it is writting a the cookie.. thus destroying all the previous data you had already saved.
(body on load)

The first step should be to check if the cookie already exists, if not, then create it smile.gif

you should create different variables to keep track of page count for each section

This post has been edited by William_Wilson: 21 Apr, 2006 - 09:41 PM
User is offlineProfile CardPM
+Quote Post

pricecd
RE: Cookies Problem
22 Apr, 2006 - 11:53 AM
Post #3

New D.I.C Head
*

Joined: 21 Apr, 2006
Posts: 2


My Contributions
so I should take the setCookies function off of the index page of my module? If I take the onload off of each page what do you suggest I do instead.

btw the onload is only on the pages of each section. The onload isn't on the index page that has the links to each part of the module.

This post has been edited by pricecd: 22 Apr, 2006 - 11:56 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Cookies Problem
22 Apr, 2006 - 12:31 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,977



Thanked: 116 times
Dream Kudos: 25
My Contributions
It's not that you have to take it off the page...you should simply check and see if a cookie already exists before writing to it...if it is already there, do not write a new one. If additional information is required in the cookie, you can append to it.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 11:31AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month