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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    onload cookie problems

    Hi I am building a site using a cookie to gather name and then insert into a form on the site later. The problem I am having with the
    Code:
    body onload="checkCookie(); ">
    however my DHTML nav menu will not load afterwards. here is the code O have.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>American Cancer Society | Relay For Life</title>
    <link rel="stylesheet" href="style.css">
    <script type="text/javascript" src="navigation.js"></script>
    <script type="text/javascript" src="cookie.js"></script>
    </head>
    
    
    <body onload="checkCookie(); ">
    
    <div id="mainContainer"> <a href="images/ACS Logo.jpg"><img src="images/ACS Logo.jpg" alt="ACS Logo" width="462" height="165"></a>
    <div id="navigation_menu">
    
    	<ul>
    		<li><a href="index.html">ACS Relay For Life</a></li>
    		<li><a href="Find_Event.html">Find An Event</a>			
    			<ul>
    				<li><a href="http://www.relayforlife.org/statesville">Statesville</a></li>
    				<li><a href="http://www.relayforlife.org/mooresville">Mooresville</a></li>
                    <li><a href="http://www.relayforlife.org/getInvolved/FindanEvent/app/findanevent.aspx">A Different Event</a></li>
    			</ul>
    		</li>
    		<li><a href="About_Relay.html">About Relay</a>
    			<ul>
    				<li><a href="mission.html">Mission</a></li>
    				<li><a href="money.html">Where Does The Money Go?</a></li>
    				<li><a href="help.html">How Can I Help?</a></li>
    			</ul>	
    		</li>
    		<li><a href="Media.html">Media</a>
            	<ul>
                	<li><a href="Photos.html">Photos</a></li>
                    <li><a href="Videos.html">Videos</a></li>
                </ul>	
    	</ul>
      </div>
    Here is the cookie.js file
    Code:
    function getCookie(c_name)
     {
     var i,x,y,ARRcookies=document.cookie.split(";");
     for (i=0;i<ARRcookies.length;i++)
       {
       x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
       y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
       x=x.replace(/^\s+|\s+$/g,"");
       if (x==c_name)
         {
         return unescape(y);
         }
       }
     }
     
    function setCookie(c_name,value,exdays)
     {
     var exdate=new Date();
     exdate.setDate(exdate.getDate() + exdays);
     var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
     document.cookie=c_name + "=" + c_value;
     }
     
    function checkCookie()
     {
     var username=getCookie("username");
     if (username!=null && username!="")
       {
       alert("Welcome again " + username);
       }
     else 
      {
       userName = prompt("What is your name?", "Enter your name here");
       if (username!=null && username!="")
         {
         setCookie("username",username,365);
         }
       }
     }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You have an error. Javascript is case sensitive. So the cookie is never set.

    userName = prompt("What is your name?", "Enter your name here");

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    mstratmann (01-21-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I fixed the error you found and still not working I also spent several more hours trying other things. From what I can tell is the problem is because I have to onload functions cannot figure out how to solve this.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>American Cancer Society | Relay For Life</title>
    <link rel="stylesheet" href="style.css">
    <script type="text/javascript" src="navagation.js"></script>
    <script type="text/javascript" src="cookie.js"></script>
    </head>
    
    
    <body onLoad="checkCookie()">
    
    <div id="mainContainer"> <a href="images/ACS Logo.jpg"><img src="images/ACS Logo.jpg" alt="ACS Logo" width="462" height="165"></a>
    <div id="navigation_menu">
    
    	<ul>
    		<li><a href="index.html">ACS Relay For Life</a></li>
    		<li><a href="Find_Event.html">Find An Event</a>			
    			<ul>
    				<li><a href="http://www.relayforlife.org/statesville">Statesville</a></li>
    				<li><a href="http://www.relayforlife.org/mooresville">Mooresville</a></li>
                    <li><a href="http://www.relayforlife.org/getInvolved/FindanEvent/app/findanevent.aspx">A Different Event</a></li>
    			</ul>
    		</li>
    		<li><a href="About_Relay.html">About Relay</a>
    			<ul>
    				<li><a href="mission.html">Mission</a></li>
    				<li><a href="money.html">Where Does The Money Go?</a></li>
    				<li><a href="help.html">How Can I Help?</a></li>
    			</ul>	
    		</li>
    		<li><a href="Media.html">Media</a>
            	<ul>
                	<li><a href="Photos.html">Photos</a></li>
                    <li><a href="Videos.html">Videos</a></li>
                </ul>	
    	</ul>
      </div>
    Attached Files Attached Files

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Well, if you have two onload functions, that will stop one of the scripts from working.

    Simply fire a set of functions in one hit when the page loads.

    Code:
    <script type="text/javascript">
    window.onload = function() {
    functionOne();
    functionTwo();
    }
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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