Current location: Hot Scripts Forums » General Web Coding » JavaScript » cookies and function


cookies and function

Reply
  #1 (permalink)  
Old 08-01-06, 08:40 PM
magicadmin magicadmin is offline
Newbie Coder
 
Join Date: Jun 2006
Location: UK
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
cookies and function

Hi all,

What i want to do is set a cookie to a time limit of 30 sec and if the cookie is set the call one function and if not call a difrent one.

How can i do this?

Thanks
__________________
Midnight Fun
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-02-06, 10:46 AM
magicadmin magicadmin is offline
Newbie Coder
 
Join Date: Jun 2006
Location: UK
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Dose any one have a clue?

Iv been at this for days now..
__________________
Midnight Fun
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-02-06, 11:05 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Please don't bump your thread before 24 hours have passed.

You can use the cookie functions from quirksmode, http://www.quirksmode.org/js/cookies.html

Changing this line:
Code:
date.setTime(date.getTime()+(days*24*60*60*1000));
To this:
Code:
date.setTime(date.getTime()+(30*1000));
And later call the function like this.

Code:
if (readCookie('yourCookie'))
{
    // Do something
}
else
{
   // Do something else
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-02-06, 06:27 PM
magicadmin magicadmin is offline
Newbie Coder
 
Join Date: Jun 2006
Location: UK
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Dose

Code:
date.setTime(date.getTime()+(30*1000));
Set the cookie?

If so what will the cookie name be for it ?

Thanks
__________________
Midnight Fun
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 08-02-06, 06:31 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
No, the createCookie() function from the page I posted above sets the cookie. You only have to change one line with the one I posted.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 08-03-06, 05:40 PM
magicadmin magicadmin is offline
Newbie Coder
 
Join Date: Jun 2006
Location: UK
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Right i dont know what im messing up... but to be frank i know fa about js... is all new to me...

Code:
function createCookie(graffiti_wall) {
	var date = new Date();
	date.setTime(date.getTime()+(30*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = graffiti_wall+"="+expires+"; path=/";
}

function readCookie(graffiti_wall) {
	var nameEQ = graffiti_wall + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function validate(f) {
    if(f['painttext'].value.split(' ').length<2) {
        toggleLayer('hiddenLayer')
		document.getElementById('bttnsubmit').disabled=true
		document.getElementById("graffiti").painttext.value=""
    } else {
		
	if (readCookie('graffiti_wall')) {
	   toggleLayer('hiddenLayerPost')
	} else {
	   toggleLayer('hiddenLayerLoad')
	   createCookie()
	}
			
	   addtag(this.value)
  }
}
Am i starting to do this in the right way ?

Im getting no errors but i still dont have a lot going on lol

Thanks
__________________
Midnight Fun

Last edited by magicadmin; 08-03-06 at 06:22 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 08-04-06, 04:39 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
The functions seem to be in order, but you are not sending a name to the createCookie function when you call it.
This means you'll check for a cookie named "graffiti_wall", and if it's not there, create a cookie with the name undefined.
Also, I suggest that you include a value for the cookie, even if it's just a 1.
Code:
function createCookie(name,value)
{
	var date = new Date();
	date.setTime(date.getTime()+(30*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}
...
createCookie('graffiti_wall',1)
__________________
[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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
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
Classified Ads skipper23 Perl 3 11-22-05 03:22 AM
PHP - page buffering - cookies problem yinyang PHP 0 11-11-05 04:08 AM
Setting a cooke (time it expires) mcrob PHP 4 04-27-05 01:13 PM
cookies misterman JavaScript 1 12-07-04 03:49 AM
Classified Ads skipper23 Perl 2 12-30-03 04:43 AM


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