William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

jQuery $.cookie: remember me

jQuery $.cookie: remember me

I found a useful article regarding on jQuery cookie’s remember me functionality: http://eisabainyo.net/weblog/2009/02/20/store-login-information-in-cookie-using-jquery/.
I did a little re-writing to make it better. E.g, by using array pm instead of scalar variables to store the small group of 3 elements seems more reasonable.

1. Store into cookies.

 $('#login_form').submit(function() {
  var expires_day = 365;
  if ($('#rememberme2').is(':checked')) {
    $.cookie('pm[email]', $('#email2').val(), { expires: expires_day });
    $.cookie('pm[password]', $('#password2').val(), { expires: expires_day });
    $.cookie('pm[remember]', true, { expires: expires_day });
  }
  else {
    // reset cookies.
    $.cookie('pm[email]', null);
    $.cookie('pm[password]', null);
    $.cookie('pm[remember]', false);
  }
  return true; //let is continue
});

2.Read from cookies

$(document).ready(function() {
  var remember = $.cookie('pm[remember]');
  if (remember) {
    $('#email2').val($.cookie('pm[email]'));
    $('#password2').val($.cookie('pm[password]'));
    $('#rememberme2').attr("checked", true);
  }
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: