Click to See Complete Forum and Search --> : Onclick set cookie and goToURL...
jsobota
04-13-2003, 10:45 AM
Hi,
I need a simple script to the following action:
When the checkbox is selected, set cookie and goto url http:///www......
The script I have runs like this:
<input type=checkbox name="francais" onClick="SetCookie('animal', this.name, exp);
Thanks
Nevermore
04-13-2003, 10:48 AM
I will need the value, name and expiry date for the cookie. Why not post the entire script you have already? I can modify it for you.
jsobota
04-13-2003, 10:57 AM
Hi Cijoori,
Many thanks. I am breaking my head with this thing.
Here you have the script:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ABLE International - choose your language</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
var expDays = 30;
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 favorite = GetCookie('animal');
if (favorite != null) {
switch (favorite) {
case 'francais' : url = 'http://www.ablexx1.com/bienvenue.htm';
break;
case 'english' : url = 'http://www.ablexx1.com/welcome.htm'; "goToURL('http://www.ablexx1.com/welcome.htm')";
break;
case 'portugues' : url = 'http://www.ablexx1.com/bemvindo.htm'; "goToURL('http://www.ablexx1.com/bemvindo.htm')";
break;
case 'espanol' : url = 'http://www.ablexx1.com/bienvenido.htm'; "goToURL('http://www.ablexx1.com/bienvenido.htm')";
break;
}
window.location.href = url;
}
// End -->
</script>
</HEAD>
<BODY bgColor=#000066>
<div align="center">
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<center>
<form>
<table> <tr><td><font face="verdana"><font size="5"><font color="#ffffff"><center><BR><BR><BR>
You are entering into the website of<br>ABLE International<p></font>
<font face="verdana"><font size="3"><font color="#ffffff"><center>Please select your language.<br>
You will need to do this selection only one time. <br>
Making a selection will place a 'Cookie' in your computer.<br>If you have cookies disabled in your browser, it will not work.<br>
<p></center>
<input type=checkbox name="francais" onClick="SetCookie('animal', this.name, exp);">Français
<input type=checkbox name="english" onClick="SetCookie('animal', this.name, exp);">English
<input type=checkbox name="portugues" onClick="SetCookie('animal', this.name, exp);">Português
<input type=checkbox name="espanol" onClick="SetCookie('animal', this.name, exp);">Español
<br>
</td></tr>
</table>
</form>
</center>
<font face="verdana"><font size="3"><font color="#ffffff"><center>If you do not wish to place a 'Cookie' in your computer,<br> click in the link below.<BR>
<center>
<font face="verdana" size="3"><font color="#ffff0">
<a href="http://www.ablexx1.com/welcome.htm">www.ablexx1.com</a></font>
</center><p>
<P><P><P>
<!-- Script Size: 2.84 KB -->
</body>
</html>
Nevermore
04-13-2003, 11:14 AM
I have created a script which will set the cookie for you: however, to move the browser the user will have to click on a go! button, or on a link. This link cannot be the radio button (I have turned the checkboxes into radios so that only one can be selected at a time). If you want to use a go! button, then you will have to use a script ing language. If your server supports PHP, I would recommend using that, as JavaScript would make the page innaccessible to 9% of visitors. The code to set the cookie is below.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ABLE International - choose your language</title>
<script language="Javascript" type="text/javascript">
<!--
function SetCookie(cookieName,cookieValue) {
var today = new Date();
var expire = new Date();
var nDays=365
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
//-->
</script>
</HEAD>
<BODY bgColor=#000066>
<div align="center">
<BODY>
<center>
<form>
<table> <tr><td><font face="verdana"><font size="5"><font color="#ffffff"><center><BR><BR><BR>
You are entering into the website of<br>ABLE International<p></font>
<font face="verdana"><font size="3"><font color="#ffffff"><center>Please select your language.<br>
You will need to do this selection only one time. <br>
Making a selection will place a 'Cookie' in your computer.<br>If you have cookies disabled in your browser, it will not work.<br>
<p></center>
<form action="">
<input type=radio name="language" onClick="SetCookie('Language','Francais');">Français
<input type=radio name="language" onClick="SetCookie('Language','English');">English
<input type=radio name="language" onClick="SetCookie('Language','Portugues');">Português
<input type=radio name="language" onClick="SetCookie('Language','Espanol');">Español
</form>
<br>
</td></tr>
</table>
</form>
</center>
<font face="verdana"><font size="3"><font color="#ffffff"><center>If you do not wish to place a 'Cookie' in your computer,<br> click in the link below.<BR>
<center>
<font face="verdana" size="3"><font color="#ffff0">
<a href="http://www.ablexx1.com/welcome.htm">www.ablexx1.com</a></font>
</center><p>
<P><P><P>
<!-- Script Size: 2.84 KB -->
</body>
</html>
jsobota
04-13-2003, 12:04 PM
Hi cijori,
thanks, you've helped me a lot. In fact, not trying to abuse your good will, if you could put the javascript GO button, that would be great. I am completely newbie to javascript, though the website was designed by me.
Jorge
Nevermore
04-13-2003, 12:09 PM
This code will move the browser on a click of the radio button (I know I said you couldn't, I was wrong). However, it will not work for everyone, so you should be sure to include some other for of navigation.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ABLE International - choose your language</title>
<script language="Javascript" type="text/javascript">
<!--
function SetCookie(cookieName,cookieValue) {
var today = new Date();
var expire = new Date();
var nDays=365
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
//-->
</script>
</HEAD>
<BODY bgColor=#000066>
<div align="center">
<BODY>
<center>
<form>
<table> <tr><td><font face="verdana"><font size="5"><font color="#ffffff"><center><BR><BR><BR>
You are entering into the website of<br>ABLE International<p></font>
<font face="verdana"><font size="3"><font color="#ffffff"><center>Please select your language.<br>
You will need to do this selection only one time. <br>
Making a selection will place a 'Cookie' in your computer.<br>If you have cookies disabled in your browser, it will not work.<br>
<p></center>
<form action="">
<input type=radio name="language" onClick="SetCookie('Language','Francais');document.location.href='Francais.htm';">Français
<input type=radio name="language" onClick="SetCookie('Language','English');document.location.href='English.htm';">English
<input type=radio name="language" onClick="SetCookie('Language','Portugues');document.location.href='Portugues.htm';">Português
<input type=radio name="language" onClick="SetCookie('Language','Espanol');document.location.href='Espanol.htm';">Español
</form>
<br>
</td></tr>
</table>
</form>
</center>
<font face="verdana"><font size="3"><font color="#ffffff"><center>If you do not wish to place a 'Cookie' in your computer,<br> click in the link below.<BR>
<center>
<font face="verdana" size="3"><font color="#ffff0">
<a href="http://www.ablexx1.com/welcome.htm">www.ablexx1.com</a></font>
</center><p>
<P><P><P>
<!-- Script Size: 2.84 KB -->
</body>
</html>
jsobota
04-13-2003, 12:26 PM
Hi Cijor,
Somehow the cookie is not set. When I return to the website, it keeps asking to set the cookie (does not go directly to the selected page). Could you take a look?
Thanks
Nevermore
04-13-2003, 12:59 PM
That isn't a fault in the script - I just didn't include a script to read the cookie. I wasn't sure if you wanted it on that page. I will have one for you soon.
Nevermore
04-13-2003, 01:05 PM
This script should do what you want.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ABLE International - choose your language</title>
<script language="Javascript" type="text/javascript">
<!--
function ReadCookie() {
var NameOfCookie="Language";
if(document.cookie.length > 0)
{
begin = document.cookie.indexOf(NameOfCookie+"=");
if(begin != -1)
{
// our cookie was set.
// The value stored in the cookie is returned from the function
begin += NameOfCookie.length + 1;
end = document.cookie.indexOf(";",begin);
if(end == -1) end = document.cookie.length;
language=(document.cookie.substring(begin,end));
if (language=="Francais")document.location.href='Francais.htm';
if (language=="English")document.location.href='English.htm'
if (language=="Espanol")document.location.href='Espanol.htm'
if (language=="Portugues")document.location.href='Portugues.htm'
}
}
}
function SetCookie(cookieName,cookieValue) {
var today = new Date();
var expire = new Date();
var nDays=365
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
//-->
</script>
</HEAD>
<BODY bgColor=#000066>
<div align="center">
<BODY onload="ReadCookie()">
<center>
<form>
<table> <tr><td><font face="verdana"><font size="5"><font color="#ffffff"><center><BR><BR><BR>
You are entering into the website of<br>ABLE International<p></font>
<font face="verdana"><font size="3"><font color="#ffffff"><center>Please select your language.<br>
You will need to do this selection only one time. <br>
Making a selection will place a 'Cookie' in your computer.<br>If you have cookies disabled in your browser, it will not work.<br>
<p></center>
<form action="">
<input type=radio name="language" onClick="SetCookie('Language','Francais');document.location.href='Francais.htm';">Français
<input type=radio name="language" onClick="SetCookie('Language','English');document.location.href='English.htm';">English
<input type=radio name="language" onClick="SetCookie('Language','Portugues');document.location.href='Portugues.htm';">Português
<input type=radio name="language" onClick="SetCookie('Language','Espanol');document.location.href='Espanol.htm';">Español
</form>
<br>
</td></tr>
</table>
</form>
</center>
<font face="verdana"><font size="3"><font color="#ffffff"><center>If you do not wish to place a 'Cookie' in your computer,<br> click in the link below.<BR>
<center>
<font face="verdana" size="3"><font color="#ffff0">
<a href="http://www.ablexx1.com/welcome.htm">www.ablexx1.com</a></font>
</center><p>
<P><P><P>
<!-- Script Size: 2.84 KB -->
</body>
</html>
jsobota
04-13-2003, 01:15 PM
Works perfect Cijor, Thanks, I owe you this.
Do you work with webdesign/systems development?
Nevermore
04-13-2003, 01:17 PM
Only in my spare time really. Why?
jsobota
04-13-2003, 01:21 PM
Well, I am setting up a consultancy company, directed to financial advisory. But now and then the clients could ask for webdesign/webdevelopment services. Would you like to discuss this further?
Nevermore
04-13-2003, 01:23 PM
I'm still in school, so it isn't really practical.
jsobota
04-13-2003, 01:25 PM
Ok. Anyway, you have my e-mail. Let's keep in touch. Thanks for your help :-)
by