Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie Redirection

Status
Not open for further replies.

Ito2233

MIS
Sep 2, 2003
77
0
0
US
I have a main page where the user selects a chocolate maker from a drop-down menu, and checks the "Remember my selection" box to have the browser set a cookie. This page doesn't work. When I select a region and click on the submit button, I get a "The page cannot be displayed" page. I don't know whether the problem is with the HTML or the Javascript. Can anyone help?

The form part is coded as follows:

Code:
<form method="post" name="cookie">
	<select name="chocolate">
		<option value="lindt" onClick="SetCookie('chocolate', this.value, exp);">Lindt</option>
		<option value="godiva" onClick="SetCookie('chocolate', this.value, exp);">Godiva</option>
		<option value="nestle" onClick="SetCookie('chocolate', this.value, exp);">Nestle</option>
	</select>
        <input type="submit" name="Submit" value="Go" onClick="Direct();"> 
        <input type="checkbox" name="checked" value="yes">
            Remember my selection
</form>

The Javascript cookie redirection code was taken from the web (the first entry that comes up when you type "cookie redirection" in Google). I modified it to use a drop-down menu, and I added code to cause redirection when the user clicks on GO.
The only part that remains to be implemented is the "Remember my selection". That part can be ignored for now. First, I want to make sure the cookie works.
THANKS!!!

Javascript code:
Code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var expDays = 180;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {  
var endstr = document.cookie.indexOf (";", offset);  
if (endstr == -1)    
endstr = document.cookie.length;  
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}
function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var type = GetCookie('chocolate');

function Direct() {
var type = GetCookie('chocolate');
if (type != null) {
switch (type) {
case 'americas' : 	url = '[URL unfurl="true"]http://www.godiva.com';[/URL] 
	     	break;
case 'canada' : 	url = '[URL unfurl="true"]http://www.lindt.com';[/URL] 
	     	break;
case 'other' : url = '[URL unfurl="true"]http://www.belimo.org';[/URL]
		break;
}
window.location.href = url;
}
}

if (type != null) {
switch (type) {
case 'americas' : 	url = '[URL unfurl="true"]http://www.godiva.com';[/URL]
	     	break;
case 'canada' : 	url = '[URL unfurl="true"]http://www.lindt.com';[/URL] 
	     	break;
case 'other' : url = '[URL unfurl="true"]http://www.belimo.org';[/URL]
		break;
}
window.location.href = url;
}
//  End -->
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top