Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    using cookies for name memory

    hi all.

    i've recently created a casino royale style website. it asks the user to enter their full name (eg, james bond) then displays welcome bond, james bond. then has a blackjack game. a fruit machine and a dice game. however each time the user returns to the lobby (the home page) they are asked to enter their name again. im familiar with javascript but have always avoided the use of cookies. i always separate my javascript page to a separate html page. what is the code i need so the usernames are remembered. also do i have a further js page or can i add it to the original js page already linked to the home page. any help with this would be hugely appreciated.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,912
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    A cookie will do the job just fine.

    Code:
    <script type = "text/javascript">
    
    function createCookie(name,value,days) {
    	if (days) {
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name) {
    	var nameEQ = name + "=";
    	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 eraseCookie(name) {
    	createCookie(name,"",-1);  // set cookie to expire yesterday
    }
    
    </script>
    
    var name = "playername" or whatever name you wish to give the cookie
    var value = "James Bond" 
    var days = number of days cookie to remain active (say 7)
    Last edited by Philip M; 04-30-2008 at 08:21 PM.

  • Users who have thanked Philip M for this post:

    fredw (04-30-2008)

  • #3
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thankyou



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •