Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_howto thread: I am having problems with cookies


Message #1 by delcyan@w... on Tue, 17 Dec 2002 21:10:08
Hello all,

I have a survey from my home page which pops up everytime someone visits 
my home page. Once a user has taken the survey, I write a cookie so the 
next time that same person visits my home page the survey will not pops up 
again.

The problem I having is that the cookie get written to the client but not 
with the name I specified. My name is HDSDec and what I see as the cookie 
name is Survey. Now, the only explaination from my part is that somehow, 
the cookie takes the name of the folder the code resides in. I have a 
folder named Survey and all the asp and htm pages are in that folder.

Please see my code below and let me know if I am doing something wrong.

Thank you

Anthony

*=*=*=*=*=*=*=*=*=*=*=*

Home Page code:

<SCRIPT LANGUAGE="JavaScript">
<!--
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// -->
</SCRIPT>

<script language="javascript">

var strWinProp

function init() {

	var inTop;
	var intLeft;
	  
	intWidth = screen.width - 10; 
	intHeight = screen.height - 80;
	intTop = intHeight - 540;
	intTop = intTop / 2;
	intLeft = intWidth - 400;
	intLeft = intLeft / 2;
	  
	strWinProp = " toolbar=no"     
	             + ",location=no"  
	             + ",directories=no"
	             + ",status=yes"    
	             + ",menubar=no"    
	             + ",resizable=no"  
	             + ",scrollbars=yes"
	             + ",titlebar=yes"  
	             + ",width=400"     
	             + ",height=540"    
	             + ",top=" + intTop         
	             + ",left=" + intLeft        
	             + "";

	if (getCookie("hdsDec1")!="Y")
	{ 
		window.open('StartingPage.htm','',strWinProp); void('');
		
	}
}
</script>

*=*=*=*=*=*=*=*=*=*=*=*=

Survey code:

<SCRIPT LANGUAGE="JavaScript">
<!--
	function setCookie(name, value, expires, path, domain, secure) 
	{  
	  var curCookie = name + "=" + escape(value) +
	      ((expires) ? "; expires=" + expires.toGMTString() : "") +
	      ((path) ? "; path=" + path : "") +
	      ((domain) ? "; domain=" + domain : "") +
	      ((secure) ? "; secure" : "");
	  document.cookie = curCookie;
	}

	function BuildCookie() 
	{
	  var now = new Date();
	  now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
	  setCookie("hdsDec1", "Y", now);
	  return true;
	}

	function Validate()
	{
		if (ValidateAnswer()==true)
		{
			BuildCookie();
			surveyForm.submit();return true;
		}
	}

//With some more code down here.

Thank you again

 


  Return to Index