Javascript Cookies

Posted on 2006-04-28
Last Modified: 2013-12-16
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" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">

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

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 =;
              = (d == 'none') ? 'block' : 'none';
                                 if ( == 'block') {
                                 //alert('here 1');
                                       document.getElementById('imgShowHide').src= 'Hide.jpg';
                                 } else {
                                  //alert('here 2');
                                       document.getElementById('imgShowHide').src= 'Show.jpg';
<body >
<form id="form1">
<script language="JavaScript">
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>');
<br /><br />

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


Question by:Lantrax
    1 Comment
    LVL 63

    Accepted Solution

    if statements need always braces.
    This is without braces:
    if ReadCookie('LeftNavState') == '' {

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
    Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
    The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
    The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now