The other day I tried to add a simple "Remember me" checkbox to the login.aspx page for a site I'm working on. Simple enough in theory that I didn't think much of it and gave myself an hour to add it in.
Here's what I needed it to do:
Sounds simple enough right? Just add a cookie and check it when login.aspx loads, right? Wrong.
The problem became clear very early on; the cookies were being cleared when the user logged out. Since my goal was to access the cookie BEFORE the user logged in, I could not rely on the cookie. But I HAD to use a cookie, there was no way around it.
So, after some searching and trial-and-errors, I came up with a simple solution: use JavaScript cookies. Now it sounds like a bit of a hack but it's not really. JavaScript has been able to read and write to cookies from the get go.
So first we need to write some simple script that will read/write to cookies:
Next, we create 2 new custom methods; one to read the cookie when the page loads and the other to write/remove the cookie when the checkbox is checked:
Now, all that is left is to add an onload event to the body tag and an onclick event to the checkbox:
(note; since the checkbox value is not used anywhere but the client, I chose not to use a .NET server control)
Viola, you're done. Simple, effective and it can be used by multiple browsers.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u