...

View Full Version : onload cookie problems



mstratmann
01-21-2012, 03:42 AM
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
body onload="checkCookie(); "> however my DHTML nav menu will not load afterwards. here is the code O have.

<!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


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);
}
}
}

Philip M
01-21-2012, 07:40 AM
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.

mstratmann
01-21-2012, 02:12 PM
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.

<!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>

Philip M
01-21-2012, 03:41 PM
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.


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum