SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Uk
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript cookies problem

    I am having problems retrieving cookie value. I've never used javascript cookies and found this script and thought I wanted to see how it works on my simple page. Here's the script


    Code:
    //capture user name
    function getUser(){
        
        var value = null;
        
        while(value==null || value==''){
            value = prompt('Please enter your name','');
        }
        
        if(value !=null || value !=''){
            createCookie(cook,value,0);
        }
    }
    
    function createCookie(cook,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=/wizzy/";
    }
    
    //read cookie
    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;
    }
    
    //disposing of cookie.
    function cookie_destroy(){
        createCookie(cook,"",-1);
    }
    And I wanted to capture and display the cookie value here:

    Code:
    <!-- page header --->
            <div id="header">
                <img src="buzz.jpg" alt="Images" align="left"width="97" height="97 /><h1 class="center"></h1>
                <span class="style3">&nbsp;
    		<script type="text/javascript">
    		    document.write(date+' '+month+', '+year+' '+hours+':'+minutes+':'+seconds+'<br/>');
    		    window.onload = getUser();
    		    
    		    getCook();
    		    
    		    var user = readCookie(name);
    			       
    		    if(user){	    
    			document.write('&nbsp; Welcome, '+user+'<br/>');		
    		    }    
    		</script>
                
                </span>
                <h3>Buzz TechNews Online</h3>
    
    
          </div><!-- end header --->
    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.onload = getUser();
    Change to
    Code:
    window.onload = getUser;
    then it actually gets called on load not immediately.

    Followed by:
    Code:
     getCook();
    
     var user = readCookie( "userName" );
    
     if( user )
     {	    
       document.write('&nbsp; Welcome, '+user+'<br/>');		
     }

    Code:
    function getUser(){
        
        var value = null;
        
        while(value==null || value=='') /* These tests need work */
        {
            value = prompt('Please enter your name','');
        }
        
        if(value !=null || value !=''){
            createCookie( "userName" ,value,0);
        }
    }
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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