logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > Javascript and JS frameworks
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 01-28-2010, 01:05 AM   #1
chriskq
chriskq's Avatar
 
Join Date: Oct 2005
Location: Sydney - DownUnder
Posts: 798
Default setting simple cookie

Hey All,

I've been out of web development for a while now so very rusty.
I need to make a cookie

1. If the user visits the site > set cookie
2. If user comes back to site > read cookie
3. If time since user last arrived is less than 24hrs > set the ID of a div to style="display:none;"


im struggling and would really appreciate some guidance.

thanks so much
__________________
can u do this with ur myspace? http://www.myspace.com/chrisjash
chriskq is offline   Reply With Quote
Old 01-29-2010, 12:58 AM   #2
chriskq
chriskq's Avatar
 
Join Date: Oct 2005
Location: Sydney - DownUnder
Posts: 798
Default

got it to work - heres my messy solution:

Code:
<HTML>
<HEAD>
<TITLE>Using functions as event handlers</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function nameDefined(ckie,nme)
{
   var splitValues
   var i
   for (i=0;i<ckie.length;++i)
   {
      splitValues=ckie[i].split("=")
      if (splitValues[0]==nme) return true
   }
   return false
}
function delBlanks(strng)
{
   var result=""
   var i
   var chrn
   for (i=0;i<strng.length;++i) {
      chrn=strng.charAt(i)
      if (chrn!=" ") result += chrn
   }
   return result
}
function getCookieValue(ckie,nme)
{
   var splitValues
   var i
   for(i=0;i<ckie.length;++i) {
      splitValues=ckie[i].split("=")
      if(splitValues[0]==nme) return splitValues[1]
   }
   return ""
}
function testCookie(cname, cvalue) {  //Tests to see if the cookie
    var cookie = document.cookie           //with the name and value
//    alert(cookie);
   var chkdCookie=delBlanks(cookie)  //are on the client computer
   var nvpair=chkdCookie.split(";")
   if(nameDefined(nvpair,cname))       //See if the name is in any pair
   {   
      tvalue=getCookieValue(nvpair,cname)  //Gets the value of the cookie
      if (tvalue == cvalue) return true
       else return false
   }
   else return false
}
function redirectLink() {
    if (testCookie("here10", "yes")) {
     //window.location="here.html"        //Go to the location indicating the user has been here
       //alert("there");
       window.document.getElementById("indicator").style.display = "none";
   }
   else{
       //alert(" not there");
       var futdate = new Date()        //Get the current time and date
      var expdate = futdate.getTime()  //Get the milliseconds since Jan 1, 1970
      expdate += 10000  //expires in 10 sec currently (milliseconds) - this is 24 hr: 24*3600*1000 
      futdate.setTime(expdate)
      var newCookie="here10=yes; path=/;"    //Set the new cookie values up
      newCookie += " expires=" + futdate.toGMTString()
      window.document.cookie=newCookie //Write the cookie
      // window.location="not.html"        //Go to the location indicating the user has not been here

      window.document.getElementById("indicator").style.display = "block";

   }
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<H3>Using an event handler to direct a link based on a cookie value</H3>
<P>
<A NAME="Here" onClick="return redirectLink()">See if you've been here.</A>
</P>

<div id="indicator" style="width:50px;height:50px;background-color:Red;display:block;">
</div>

<SCRIPT LANGUAGE="JavaScript">
    redirectLink();
</SCRIPT>



</BODY>
</HTML>
__________________
can u do this with ur myspace? http://www.myspace.com/chrisjash
chriskq is offline   Reply With Quote
Reply


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 On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:35 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com