I have the following code I am using to writing and reading cookies:

//Get Villages from cookie
	function getVillageFromCookie(villageCount)
	{
		var currentVillage = -1;
		var villagesCookie = getCookie("tw_ratio_villageCount");
		if (villagesCookie!=null && villagesCookie!="")
		{
			currentVillage = villagesCookie;
		}
		else 
		{
			storeCookie("tw_ratio_villageCount", villageCount, 1);
			currentVillage = 1;
		}
		
		return currentVillage;		
	}
	
	//Get troops from cookie
	function getTroopsFromCookie()
	{
		var rtnVal = 0;
		var presavedTroops = getCookie("tw_ratio_troops");
		if(presavedTroops != null && presavedTroops != "")
		{
			rtnVal = parseInt(presavedTroops);
		}
		
		return rtnVal;
	}
	
	//Store the passed number of troops in a cookie
	function storeCookie(cookieName, troopNum, exdays)
	{
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(troopNum) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie =  cookieName + "=" + c_value;
	}
	
	//Get Cookie Function
	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);
			}
		  }
	}

Since, I am not so proficient in javascript i used the code provided at w3schools.com

My aim is to store 2 variables in cookies. The first is an integer representing the current village. The other is the integer that represent troop count. They are used as below:

var points = getPoints(true);
		var troops = 0;
		//alert(troops);
		if(villages > 1)
		{
			var currentVillage = getVillageFromCookie(villages);
			troops = getTroops(true); //returns an int
			if(villages == currentVillage)
			{
				troops += getTroopsFromCookie();
			} else
			{
				var previousTroops = getTroopsFromCookie(villages);
				previousTroops += troops;
				storeTroops(troops, 1);
				storeVillageCountInCookie(currentVillage++, 1);
				alert("Please execute the script from the next village's recruit screen.");
			}
			
		} else
		{
			troops = getTroops(true);
		}
		alert("Total Points: " + points + "\nTotal Troops: " + troops + "\nRatio: " + (troops/points).toFixed(4));

However, Firebug is throwing this error: missing } in compound statement I am sure this is the part of the code causing the error because it works when this code is removed. Suggestion?

Member Avatar for stbuchok

It looks like there is more code that you aren't showing that is causing the issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.