|
Post by Brent on Feb 3, 2008 20:22:29 GMT -5
' Cookies by Javascript ' By Brent D. Thorn, 2/2008
[start]
cls
html "<h1>Please log on.</h1>"
call enableCookies
html " <table border=""0""> <tr> <td>Username:</td> <td>" textbox #user, "" html " </td> </tr> <tr> <td>Password:</td> <td>" passwordbox #pass, "" html " </td> </tr> <tr> <td>" link #logon, "Log on", [logOn] html " </td> <td>" checkbox #reme, "Remember me", 0 html " </td> </tr> </table>"
html "<script type=""text/javascript""> <!-- if (getCookie('reme')) { var inps = document.getElementsByTagName('INPUT'); var user, pass, reme; for (var i = 0; i < inps.length; i++) { switch (inps.type) { case 'text': user = inps; break; case 'password': pass = inps; break; case 'checkbox': reme = inps; break; } } user.value = getCookie('user'); pass.value = getCookie('pass'); reme.checked = true; } //--> </script>"
wait
[logOn]
user$ = #user contents$() pass$ = #pass contents$() reme = #reme value()
cls
html "<h1>Welcome, "+user$+"!</h1>"
call enableCookies
if reme then 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 html "<script type=""text/javascript""> <!-- var path = ''; // root delCookie('reme', path); delCookie('user', path); delCookie('pass', path); //--> </script>" end if
link #off, "Log off", [logOff]
wait
[logOff]
cls
html "<h1>Come back soon!</h1>"
link #backon, "Log back on again", [start]
wait
sub enableCookies ' The following JS was taken from a website that I don't recall many years ago.
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 |
|
|
|
Post by Alyce on Feb 4, 2008 5:39:39 GMT -5
This would be a great demo for the wiki!
|
|
|
Post by Brent on Feb 7, 2008 16:02:56 GMT -5
|
|
|
Post by votan on Sept 8, 2008 13:07:16 GMT -5
Can anyone help me to get this to work?? (I have no idea about JS) But I know that the above script can't work directly.... but still, I added the script to my code. Cause maybe some slight changes will already make it work!? What I need to do is.... remember the user$, pass$ and the checkbox value, but NOT the captcha code. Would be cool if someone could help.[login] cls Call enableCookies html "<h1 align = 'center'>Login</h1>" html "<p align = 'center'>" html "</p>" html "<table align = 'center' border = '1'>" html "<tr>" html "<td align = 'center' valign = 'top'>" if logincount <> 20 then print "username : "; textbox #user, "" html "<br>" print "password : "; passwordbox #pass, "" html "<br>" print "captcha. : "; textbox #captchacode, "" html "<br>" button #submit, "Submit", [checklogin] html "<br>" checkbox #reme, "Remember me", 0 else html "<br>" html "<b align = 'center'>Login suspended for 60 minutes</b>" html "<br> " end if html "</td>" html "</tr>" html "</table>" html "<script type=""text/javascript""> <!-- if (getCookie('reme')) { var inps = document.getElementsByTagName('INPUT'); var user, pass, reme; // Find controls. for (var i = 0; i < inps.length; i++) { switch (inps[i].type) { case 'text': user = inps[i]; break; case 'password': pass = inps[i]; break; case 'checkbox': reme = inps[i]; break; } } user.value = getCookie('user'); pass.value = getCookie('pass'); reme.checked = true; } //--> </script>" wait
[checklogin] user$ = #user contents$() pass$ = #pass contents$() reme = #reme value() Call enableCookies 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
goto [again] [again] link #again, "reload login", [login] 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
|
|
|
Post by Brent on Sept 9, 2008 16:36:43 GMT -5
Hello,
I believe the problem is with the kludge I used to gain access to the widgets. It will find only the last of any type. The quick fix is to changeuser = inps[i]; break; toif (!user) user = inps[i]; break; If you have a RB 1.01 beta release, a much better solution is available. I will change the code on the wiki once 1.01 is released.
|
|
|
Post by votan on Sept 9, 2008 16:59:27 GMT -5
Great, thanx! This fixes the problem. And yes, I'm using 1.01B2.... so I guess I'll wait for your new version to implement it for a final version. Do you also know how to do a little AJAX? Want to use it to add/delete items from a personal DB-driven inventory without page refresh. And a small changing immage should indicate if it is in your inventory or not. (And all this unobstrusive, so that it also still works when JS is disabled... just with pagerefresh then...)
|
|