Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: cookies


Message #1 by "Ian Wood" <wg.mail@b...> on Thu, 15 Mar 2001 15:48:01
If you just want to get/set a cookie temporarily here's one way to do it:

function SetCookie(name, value) {
	// lasts as long as the browser is open
	document.cookie = name + "=" + escape(value);
}

function GetCookie(name) {
	var result = null;
	var theCookie = " " + document.cookie + ";";
	var searchName = " " + name + "=";
	var startOfCookie = theCookie.indexOf(searchName);
	var endOfCookie;
	
	if (startOfCookie != -1) {
	
		startOfCookie += searchName.length;
		endOfCookie = theCookie.indexOf(";", startOfCookie);
		result = unescape(theCookie.substring(startOfCookie, endOfCookie));
	}
	return result;
}
> I'm new to the JavaScript world (3 days), so my question is probably very 
> easy. My server doesn't support custom scripts, so all scripting has to be 
> done client-side. I need to secure part of a web site with a password. 
> I've got a routine that takes the contents of a password text box and 
> transforms it into a number. This number is visible in VIEW---SOURCE, but 
> not the actual password, so it's a little more secure. This number is then 
> set as the value in a cookie. To fully simulate ASP support, any pages 
> below the Log In one need to check that the cookie exists before granting 
> access. I've tried everything, but nothing that gives the equivalent to 
> Response.Cookies("cookiename") ="value" is readily apparant. I've tried a 
> couple of functions, but with no joy.
> 

  Return to Index