|
Bay Six Software Beyond the Basics
|
View previous topic :: View next topic |
Author |
Message |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 800
|
Posted: Sep 12th, 2008, 7:32pm Post subject: [DEMO] RB Cookies by Javascript, revised |
|
|
Code: | ' Cookies by Javascript
' By Brent D. Thorn, 2/2008
' Revised 9/2008 for RB 1.01
' Revised 1/2009 to simplify the code
[start]
Cls
Call enableCookies
HTML "<h1>Please log in.</h1>"
Print "Username:"
TextBox #user, ""
Print
Print "Password:"
PasswordBox #pass, ""
Print
Checkbox #reme, "Remember me", 0
Print
Button #login, "Log in", [logIn]
HTML "
<script type=""text/javascript"">
<!--
if (getCookie('reme')) {
var user = document.getElementById('#user');
var pass = document.getElementById('#pass');
var reme = document.getElementById('#reme');
user.value = getCookie('user');
pass.value = getCookie('pass');
reme.checked = true;
}
//-->
</script>"
Wait
[logIn]
user$ = #user Contents$()
pass$ = #pass Contents$()
reme = #reme Value()
'TODO: Authenticate user.
Cls
Call enableCookies
HTML "<h1>Welcome, "+user$+"!</h1>"
If reme Then
' Save info in a cookie.
HTML "
<script type=""text/javascript"">
<!--
var path = ''; // root
var expire = days(30);
setCookie('reme', true, path, expire);
setCookie('user', '"+user$+"', path, expire);
setCookie('pass', '"+pass$+"', path, expire);
//-->
</script>"
Else
' Delete info from cookie.
HTML "
<script type=""text/javascript"">
<!--
var path = ''; // root
delCookie('reme', path);
delCookie('user', path);
delCookie('pass', path);
//-->
</script>"
End If
Link #logout, "Log out", [logOut]
Wait
[logOut]
Cls
HTML "<h1>Come back soon!</h1>"
Link #again, "Log in again", [start]
Wait
Sub enableCookies
HTML "
<script type=""text/javascript"">
<!--
function getCookie(name) {
var dc = document.cookie;
var cname = name + ""="";
var clen = dc.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + cname.length;
if (dc.substring(cbegin, vbegin) == cname) {
var vend = dc.indexOf("";"", vbegin);
if (vend == -1) vend = clen;
return unescape(dc.substring(vbegin, vend));
}
cbegin = dc.indexOf("" "", cbegin) + 1;
if (cbegin == 0) break;
}
return null;
}
function setCookie(name, value, path, expires) {
if (!expires) expires = new Date();
document.cookie = name+""=""+escape(value)+""; " +_
"expires=""+expires.toGMTString()+""; " +_
"path=/""+path;
}
function delCookie(name, path) {
document.cookie = name+""=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/""+path;
}
function days(num) {
var d = new Date();
d.setTime(d.getTime()+(86400000*num));
return d;
}
//-->
</script>"
End Sub
|
_________________ Brent
Last edited by Brent on Jan 18th, 2009, 7:17pm; edited 2 times in total |
|
Back to top |
|
|
STPendl Full Member
Joined: 20 Aug 2007 Posts: 161 Location: Austria
|
Posted: Sep 13th, 2008, 7:20am Post subject: Re: [DEMO] RB Cookies by Javascript, revised |
|
|
To delete the cookie it should be sufficient to use Code: | function delCookie(name, path) {
expires = days(-1);
document.cookie = name+""=; expires=""+expires.toGMTString()+""; path=/""+path;
}
|
_________________ Stefan
Any code I post can be freely used, just give credit. |
|
Back to top |
|
|
Alyce Full Member
Joined: 04 Jul 2005 Posts: 91
|
Posted: Sep 15th, 2008, 1:45pm Post subject: Re: [DEMO] RB Cookies by Javascript, revised |
|
|
Wow. Cool. Thanks for this one. _________________ - Alyce |
|
Back to top |
|
|
Brent Site Admin
Joined: 01 Jul 2005 Posts: 800
|
Posted: Jan 7th, 2009, 9:34pm Post subject: Re: [DEMO] RB Cookies by Javascript, revised |
|
|
I just updated the code, eliminating the table on the login screen in order to simplify the demo and make it much clearer. If no one finds any problems, I'll update the wikispaces page in a few days. |
|
Back to top |
|
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|
|