Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-21-2012, 03:42 AM   PM User | #1
mstratmann
New to the CF scene

 
Join Date: Jan 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
mstratmann is an unknown quantity at this point
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);
     }
   }
 }
mstratmann is offline   Reply With Quote
Old 01-21-2012, 07:40 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
mstratmann (01-21-2012)
Old 01-21-2012, 02:12 PM   PM User | #3
mstratmann
New to the CF scene

 
Join Date: Jan 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
mstratmann is an unknown quantity at this point
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
File Type: zip cookie.zip (568 Bytes, 9 views)
File Type: zip navigation.zip (2.6 KB, 5 views)
mstratmann is offline   Reply With Quote
Old 01-21-2012, 03:41 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 +1. The time now is 10:26 PM.


Advertisement
Log in to turn off these ads.