Javascript Cookies

When i take the cookie part out i don't get an error. When i add it i do.

I want to set the cookie and read it.

THis only has to work in IE


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<script language="javascript">

if ReadCookie('LeftNavState') == '' {
      SetCookie( 'LeftNavState', 'none', '1' );
      
      alert(ReadCookie('LeftNavState'));
}


function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return "";
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length;
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}



                       function toggleDisplay(id)
                       {
                       var elm = document.getElementById(id);
                       if (elm != null)
                       {
                       var d = elm.style.display;
                       elm.style.display = (d == 'none') ? 'block' : 'none';
                       
                                 if (elm.style.display == 'block') {
                                 //alert('here 1');
                                       document.getElementById('imgShowHide').src= 'Hide.jpg';
                                 } else {
                                  //alert('here 2');
                                       document.getElementById('imgShowHide').src= 'Show.jpg';
                                 }
                                
                                
                                 }
                       }
                                
                       </script>
<body >
<form id="form1">
<script language="JavaScript">
<!--
testValue=Math.floor(1000*Math.random());
SetCookie('AreCookiesEnabled',testValue);
if (testValue==ReadCookie('AreCookiesEnabled'))
     document.write('<b>Cookies are currently enabled in your browser.</b>');
else document.write('<b>Cookies are not currently enabled in your browser.</b>');
//-->
</script>
<br /><br />

<table>
<tr>
<td id="tdLeftNav"><a href="page2.html">Page 2</a></td>
<td>
<div onclick="toggleDisplay('tdLeftNav');" style="CURSOR: hand">
<img src="Hide.jpg" border="0" id="imgShowHide" name="imgShowHide" />
</div>

</td></tr>

</table></form>
</body>
</html>
LantraxAsked:
Who is Participating?
 
ZvonkoConnect With a Mentor Systems architectCommented:
if statements need always braces.
This is without braces:
if ReadCookie('LeftNavState') == '' {

Correct is this:
if (ReadCookie('LeftNavState') == '') {

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.