[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript Cookies

Posted on 2006-04-28
1
Medium Priority
?
295 Views
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" "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>
0
Comment
Question by:Lantrax
1 Comment
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 16563121
if statements need always braces.
This is without braces:
if ReadCookie('LeftNavState') == '' {

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

0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
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…
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 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